vfgjrk
vfgjrk

Reputation: 443

Execute .jar file from a Java program

How could I run a local jar file from a java program?

The jar file is not in the class-path of the Java caller program.

Upvotes: 44

Views: 100047

Answers (10)

Joe Almore
Joe Almore

Reputation: 4331

This is my appriach, which I consider is more complete:

public static Process exec(String path, String filename) throws IOException {
    String javaHome = System.getProperty("java.home");
    String javaBin = javaHome +
            File.separator + "bin" +
            File.separator + "java";

    ProcessBuilder pb = new ProcessBuilder(javaBin, "-jar", path+filename);
    return pb.start();
}

Upvotes: 1

Mzzl
Mzzl

Reputation: 4126

To run an executable jar from inside your java application, you can copy the JarClassLoader from https://docs.oracle.com/javase/tutorial/deployment/jar/examples/JarClassLoader.java

Use it like this. 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);

Keep in mind that you're now inserting someone else's binary into your code. If it gets stuck in an infinite loop, your Thread hangs, if it calls System.exit(), your JVM exits.

Upvotes: 1

  1. Add jar library to your project
  2. Import main class (see manifest in jar file)
  3. Invoke static method main with arguments

    String args[] = {"-emaple","value"};
    PortMapperStarter.main(args);
    

Upvotes: 1

Syeda Zunaira
Syeda Zunaira

Reputation: 5207

You can run a jar file from where ever you want by using only this one line code.

    Desktop.getDesktop().open(new File("D:/FormsDesktop.jar"));

where

new File("your path to jar")

Hope it helps.

Thanks.

Upvotes: 1

Anderson Lopes
Anderson Lopes

Reputation: 639

    Process proc = Runtime.getRuntime().exec("java -jar Validate.jar");
    proc.waitFor();
    // Then retreive the process output
    InputStream in = proc.getInputStream();
    InputStream err = proc.getErrorStream();

    byte b[]=new byte[in.available()];
    in.read(b,0,b.length);
    System.out.println(new String(b));

    byte c[]=new byte[err.available()];
    err.read(c,0,c.length);
    System.out.println(new String(c));

Upvotes: 15

venkatram mutyala
venkatram mutyala

Reputation: 3

1) Set the class path from environment variables

2) Go to the folder where your jar file exists

3) Run the following commands through command prompt

java -jar jarfilename

Upvotes: -4

Johnydep
Johnydep

Reputation: 6277

Another way to do on windows is:

Runtime.getRuntime().exec("cmd /c start jarFile");

this way you can set priority of your process as well (normal/low/etc)

Upvotes: 2

Chris Dennett
Chris Dennett

Reputation: 22721

Could something like the following be useful?

http://download.oracle.com/javase/tutorial/deployment/jar/jarclassloader.html

Upvotes: 2

Nikola Yovchev
Nikola Yovchev

Reputation: 10216

First, the description of your problem is a bit unclear. I don't understand if you want to load the classes from the jar file to use in your application or the jar contains a main file you want to run. I will assume it is the second.

If so, you have a lot of options here. The simplest one would be the following:

String filePath; //where your jar is located.
Runtime.exec(" java -jar " + filepath);

Voila... If you don't need to run the jar file but rather load the classes out of it, let me know.

Upvotes: 3

aioobe
aioobe

Reputation: 420951

I suggest you use a ProcessBuilder and start a new JVM.

Here is something to get you started:

ProcessBuilder pb = new ProcessBuilder("/path/to/java", "-jar", "your.jar");
pb.directory(new File("preferred/working/directory"));
Process p = pb.start();

Upvotes: 52

Related Questions