Execute another jar in a Java program

run a jar file from another java program
how to run a java program from another java program
java processbuilder run jar file
java load class from jar
powershell execute java -jar with parameters
run java program with multiple jar files
java create jar from class
java run main class from command line

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 run-time process detail in my GUI program.

Any suggestion?

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.

Execute another jar in a java program, 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  Simply construct a URLClassLoader with the jar as one of the URL. Then call its forClass () if you know the class name (full name of course). Or inspect the manifest file using its 'findResources (String name)'. Once you get the class, you can use reflection to get its static method main.

.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));

How do I run another application from Java?, This Java tutorial describes using Runtime's exec method to run another application We'll create a TestOutput class with a main method that outputs a line to  In this snippet, jarUrl is the URL to download the jar from, for example file:/tmp/my-jar.jar and args is the array of strings you want to pass as command line arguments to the jar. JarClassLoader loader = new JarClassLoader(jarUrl); String main = loader.getMainClassName(); loader.invokeClass(main, args);

Hope this helps:

public class JarExecutor {

private BufferedReader error;
private BufferedReader op;
private int exitVal;

public void executeJar(String jarFilePath, List<String> args) throws JarExecutorException {
    // Create run arguments for the

    final List<String> actualArgs = new ArrayList<String>();
    actualArgs.add(0, "java");
    actualArgs.add(1, "-jar");
    actualArgs.add(2, jarFilePath);
    try {
        final Runtime re = Runtime.getRuntime();
        //final Process command = re.exec(cmdString, args.toArray(new String[0]));
        final Process command = re.exec(actualArgs.toArray(new String[0]));
        this.error = new BufferedReader(new InputStreamReader(command.getErrorStream()));
        this.op = new BufferedReader(new InputStreamReader(command.getInputStream()));
        // Wait for the application to Finish
        this.exitVal = command.exitValue();
        if (this.exitVal != 0) {
            throw new IOException("Failed to execure jar, " + this.getExecutionLog());

    } catch (final IOException | InterruptedException e) {
        throw new JarExecutorException(e);

public String getExecutionLog() {
    String error = "";
    String line;
    try {
        while((line = this.error.readLine()) != null) {
            error = error + "\n" + line;
    } catch (final IOException e) {
    String output = "";
    try {
        while((line = this.op.readLine()) != null) {
            output = output + "\n" + line;
    } catch (final IOException e) {
    try {
    } catch (final IOException e) {
    return "exitVal: " + this.exitVal + ", error: " + error + ", output: " + output;

Best way executing jar from java code then , I've never tried it. What happens when the Monitor class does a System.exit() after starting a process? Is the process ended also? Stack Overflow Public questions and answers; Run a jar File from java program. I am trying to execute jar files from another Java program. I am using the

Execute another jar in a Java program, run a jar file from another java program java processbuilder run jar file how to run a java program from another java program powershell execute java -jar with  We'll use the -cp option (short for classpath) to specify the JAR file that contains the class file we want to execute: java -cp jar-file-name main-class-name [args …] As you can see, in this case, we'll have to include the main class name in the command line, followed by arguments.

The following works by starting the jar with a batch file, in case the program runs as a stand alone:

public static void startExtJarProgram(){
        String extJar = Paths.get("C:\\absolute\\path\\to\\batchfile.bat").toString();
        ProcessBuilder processBuilder = new ProcessBuilder(extJar);
        processBuilder.redirectError(new File(Paths.get("C:\\path\\to\\JavaProcessOutput\\extJar_out_put.txt").toString()));
        try {
           final Process process = processBuilder.start();
            try {
                final int exitStatus = process.waitFor();
                    System.out.println("External Jar Started Successfully.");
                    System.exit(0); //or whatever suits 
                    System.out.println("There was an error starting external Jar. Perhaps path issues. Use exit code "+exitStatus+" for details.");
                    System.out.println("Check also C:\\path\\to\\JavaProcessOutput\\extJar_out_put.txt file for additional details.");
            } catch (InterruptedException ex) {
                System.out.println("InterruptedException: "+ex.getMessage());
        } catch (IOException ex) {
            System.out.println("IOException. Faild to start process. Reason: "+ex.getMessage());
        System.out.println("Process Terminated.");

In the batchfile.bat then we can say:

@echo off
start /min C:\path\to\jarprogram.jar

run jar from java application (Java in General forum at Coderanch), What i can't figure out is how i can run the file "mainApp.jar" from getRuntime().​exec("cmd /C java -jar 'C://Program Files//Frontlinesystems/mainApp.jar' "); Another, completely different option, is to not use Runtime.exec  It simply compiles and executes another java program. I am using Runtime.exec() function to compile and run. There is no problem with compilation. but when it runs, if the second program needs an input to read from keyboard, I can't give it from the master process. I used getOutputStream() function. but it couldn't help.

Execute another jar in a Java program, 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  My aim is to run a java program from another one. It is still unclear if you are trying to compile a java file or trying to run a java program. You get better help if you explain your question better. javac compiles .java files, it does not run a java file

Run a Java Application from the Command Line, Later, we'll learn how to run a JAR application with or without hardcoded values, and it still can handle many different use cases. As we have already discussed, the entry point of a Java main class is the main method. To get this file looking like a java file and opening with java by default, simply right click on the jar file and select open with, then click on choose another app. Here select Java Platform SE Binary, the check the box where it says always use this app to open.jar file.

Calling an External Program in Java using Process and Runtime , On running the code, an instance of Google Chrome opens up on the computer. Another way to create an external process is using ProcessBuilder which has  Have you ever thought if it’s possible to compile and run a java program from another java program? We can use Runtime.exec(String cmd) to issue commands to the underlying operating system. We will use the same approach to compile and run a java program from another java program.