0

I have two java applications (1,2), and want to run application 2 from application 1. First I tried to create a .jar file of application 2 and run it with ProcessBuilder. That worked until I wanted to search for something inside application 2, something with packages missing and stuff.

Then I thought instead I could just run application2.java from ProcessBuilder in application 1, but that doesn't seem to be the right way to do it? Now I get errors, missing packages but in a earlier stage (the application is not started at all).

I'm doing this in Ubuntu. Any tips?

10
  • Should app2 be an independent application or is it an extension/plugin of app1? If the latter is the case, loading the jar in a new classloader in app1 is preferable. In the primer case should they communicate with each other (use serialization or sockets) or is app2 simply calculating something which it returns as output? Latter case: Process proc = Runtime.getRuntime().exec("/bin/bash","-c","java -jar App2.jar parameters passed to the app"); // read the input stream obtained by proc.getInputStream() Commented Jan 9, 2014 at 10:20
  • Application 2 uses a XML file made by application 1 to visualize specific terms from Wordnet. I have tried this: ProcessBuilder pb = new ProcessBuilder("myjar.jar"); and this runs the jar and opens the program. But then, I get some errors when application 2 tries to locate wordnet resources: Exception in thread "AWT-EventQueue-0" edu.smu.tspell.wordnet.impl.file.RetrievalException: Error opening index file: resources/WordNet-3.0/dict/index.sense (No such file or directory) I'm pretty sure that wordnet files are included in the jar, but maybe paths are wrong somehow.. Commented Jan 9, 2014 at 10:38
  • resources/WordNet-3.0/dict/index.sense sounds like a maven-like structure. I'm rather sure there is no resources/.. directory within your jar therefore it won't be able to locate the resource. Open the app2.jar with f.e. 7-zip and see if there is a /Wordnet-3.0/dict/index.sense entry - if so, you need to modify your app2 to load the resources via getClass().getResource("/WordNet-3.0/dict/index.sense"); rather than try to load it via src/resources/... Commented Jan 9, 2014 at 10:43
  • I added a empty folder resources (in intelliJ) and added wordnet inside that map hoping that it would work, therefore resources/wordnet-3.0/dict/index.sense exists. Commented Jan 9, 2014 at 10:48
  • just because it exists in IntelliJ does not mean it exists inside the jar and the jar is trying to find the required resources not IntelliJ! On using maven for dependency management and/or deployment reasons, you will usually have a project structure which contains a /src/main/resources/... structure where each file listed in this directory is copied into the jar - as you probably not use something like this, how should this get copied into the jar? Commented Jan 9, 2014 at 10:56

1 Answer 1

1

Add app. 2 to the run-time class-path of app. 1 and call its classes or methods directly, would be the easiest way with the best feedback (i.e. instead of getting 'exit value 1' you might see NoClassDefFoundError ThisIsTheMissingClassName).

Sign up to request clarification or add additional context in comments.

5 Comments

Ok, now the two applications are in different workspaces, does that cause any trouble? In IntelliJ, I go to Build - Edit configurations, but I can't add anything that's not in the same workspace. If this is the correct way of course?
Have you heard the expression 'The tail wagging the dog'? It seems to apply here, where IntelliJ is the tail, and you are the dog. I had presumed you were doing this in a circumstance where you have 2 (or more) Jars, and don't really think it has anything to do with your IDE. I mean, think about it, will the end user be expected to install IntelliJ to run app. 1?
No thats not the ideal thing, but now the exporting of application 2 to a .jar gives me errors about missing packages. This application is not made by me, and some of the dependencies that is causing the missing packages is external jars inside that application. That's why I would like to run this from the IDE, and maybe later I can focus on how to create the Jars (which indeed is the best and only good solution).
In that case perhaps you should ask an entirely separate question with the IntelliJ tag attached. While I'm sure it is a great IDE (I've heard it is better than either Netbeans or Eclipse, both of which I use) I don't use IntelliJ, and don't provide help with IDEs at all. I feel my time and expertise is better spent on my strong topics - Java and Swing.
If app2 requires further libraries you need to add these libraries to the path while invoking the jar: jar -cp requiredLib.jar;app2.jar main.class.of.app2 or modify the MANIFEST.MF of your app2.jar accordingly

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.