execute another jar in a java program

execute another jar in a java program  using -'java'

I had written several simple java applications named as A.jar, B.jar. Now i want to write a GUI java program so that user can press button A to execute A.jar and button B to execute B.jar .Also i want to output the runtime process detail in my GUI program. Any suggestion?

asked Sep 15, 2015 by CNMLie
0 votes

5 Answers

0 votes

If I understand correctly it appears you want to run the jars in a separate process from inside your java GUI application.

To do this you can use:

// Run a java app in a separate system process
Process proc = Runtime.getRuntime().exec("java -jar A.jar");
// Then retreive the process output
InputStream in = proc.getInputStream();
InputStream err = proc.getErrorStream();

Its always good practice to buffer the output of the process.

answered Sep 15, 2015 by JoleneZ95zmv
0 votes

You can create your own classloader which knows about the jar-file in question, and let it call the main method in A.jar or B.jar respectively. Then when you are done, you can discard the classloader.

Note: This runs inside the current JVM and does not protect the main application from the new code. It is handy for adding new functionality to a running program.

answered Sep 15, 2015 by FYWJessievyt
0 votes

.jar isn't executable. Instantiate classes or make call to any static method.

EDIT: Add Main-Class entry while creating a JAR.

>p.mf (content of p.mf)

Main-Class: pk.Test


package pk;
public class Test{
  public static void main(String []args){
    System.out.println("Hello from Test");

Use Process class and it's methods,

public class Exec
   public static void main(String []args) throws Exception
        Process ps=Runtime.getRuntime().exec(new String[]{"java","-jar","A.jar"});
        java.io.InputStream is=ps.getInputStream();
        byte b[]=new byte[is.available()];
        System.out.println(new String(b));
answered Sep 15, 2015 by AlonzoSGUSc
0 votes

If you are java 1.6 then the following can also be done:

import javax.tools.JavaCompiler; 
import javax.tools.ToolProvider; 

public class CompilerExample {

    public static void main(String[] args) {
        String fileToCompile = "/Users/rupas/VolatileExample.java";

        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();

        int compilationResult = compiler.run(null, null, null, fileToCompile);

        if (compilationResult == 0) {
            System.out.println("Compilation is successful");
        } else {
            System.out.println("Compilation Failed");
answered Sep 15, 2015 by AntonietXxo
0 votes

If the jar's in your classpath, and you know its Main class, you can just invoke the main class. Using DITA-OT as an example:

import org.dita.dost.invoker.CommandLineInvoker;
CommandLineInvoker.main('-f', 'html5', '-i', 'samples/sequence.ditamap', '-o', 'test')

Note this will make the subordinate jar share memory space and a classpath with your jar, with all the potential for interference that can cause. If you don't want that stuff polluted, you have other options, as mentioned above - namely:

  • create a new ClassLoader with the jar in it. This is more safe; you can at least isolate the new jar's knowledge to a core classloader if you architect things with the knowledge that you'll be making use of alien jars. It's what we do in my shop for our plugins system; the main application is a tiny shell with a ClassLoader factory, a copy of the API, and knowledge that the real application is the first plugin for which it should build a ClassLoader. Plugins are a pair of jars - interface and implementation - that are zipped up together. The ClassLoaders all share all the interfaces, while each ClassLoader only has knowledge of its own implementation. The stack's a little complex, but it passes all tests and works beautifully.
  • use Runtime.getRuntime.exec(...) (which wholly isolates the jar, but has the normal "find the application", "escape your strings right", "platform-specific WTF", and "OMG System Threads" pitfalls of running system commands.
answered Sep 15, 2015 by MyrtisEspie