Assume that I have a java program A. The Java program A needs to call another java program B(jar), passes arguments to it and receives the return value from B. How can I achieve this?
Asked
Active
Viewed 4,450 times
0
-
What development environment are you using? – hsun324 Feb 04 '14 at 02:49
-
You could load `B` into a custom class loader and run the classes directory, if you know what they are. All you could use `ProcessBuilder` to run `java -jar B.jar ....` – MadProgrammer Feb 04 '14 at 02:53
-
Do you need program B to stay in a separate jar? If not, you should consider importing the contents into your library, and then building them into the jar for program A. If you're using netbeans, or anything that uses Ant, [this](http://stackoverflow.com/questions/10834589/netbeans-how-can-i-include-external-jar-fileslibraries-in-the-jar-file-of-my) might help. – Loktopus Feb 04 '14 at 02:56
3 Answers
1
The easiest solution would be to have the jar as a build-time dependency and invoke it statically from your code.
Please check this: How to run a jar file from a separate jar file?
The above is more applicable for you compared to Execute another jar in a java program

Community
- 1
- 1

user2221125
- 156
- 1
- 2
- 17
0
I believe your question is answered here Execute another jar in a java program depending on your runtime environment you may need to specify a path to java and/or the jar file
0
All we have to do is introduce an entry into the JAR file's manifest (MANIFEST.MF in the JAR's META-INF subdirectory), like
Main-Class: com.tedneward.jars.Hello
All a user has to do to execute the JAR file now is specify its filename on the command-line, via java -jar outapp.jar.

vinayknl
- 1,242
- 8
- 18