44

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.

Java Is Cool
  • 552
  • 2
  • 5
  • 16
vfgjrk
  • 443
  • 1
  • 4
  • 4

10 Answers10

52

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();
aioobe
  • 413,195
  • 112
  • 811
  • 826
  • @aioobe Can I integrate the System.out in the original program? excuse the question but I'm new in java – vfgjrk Feb 08 '11 at 17:35
  • 1
    Not sure I understand your comment, but you get a `Process` back from `start()` which provides you with `getInputStream` / `getOutputStream`. – aioobe Feb 08 '11 at 17:37
  • Sorry again .. and if I wanted to pass a parameter to the jar as do I? – vfgjrk Feb 08 '11 at 17:54
  • 1
    You just add more arguments to the `ProcessBulider` constructor: `new ProcessBuilder("/path/to/java", "-jar", "your.jar", "arg1", "arg2");` – aioobe Feb 08 '11 at 17:57
15
    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));
Anderson Lopes
  • 639
  • 7
  • 10
3

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.

Nikola Yovchev
  • 9,498
  • 4
  • 46
  • 72
  • 2
    Please drag yourself (kicking and screaming - if need be) into the abilities of Java 1.5 & ProcessBuilder. – Andrew Thompson Feb 08 '11 at 18:15
  • This solution will do the same as the `ProcessBuilder` one (except if you want to close, etc.), and you need to type less. In my cause I'll need a launcher that closes itself after opening a jar, so that is enough for me. –  Jul 13 '17 at 14:35
  • Please keep in mind that ProcessBuilder is the now recommended way to invoke a binary and that there are new enhancements coming in java 9: http://www.javaworld.com/article/3176874/java-language/java-9s-other-new-enhancements-part-3.html. There is already an article that discusses the difference between Runtime and ProcessBuilder: https://stackoverflow.com/questions/6856028/difference-between-processbuilder-and-runtime-exec. – Nikola Yovchev Jul 14 '17 at 11:07
  • This approach won't work if you need to specify working directory. ProcessBuilder helped me (as suggested in the answer above). – Andrey Kotov Mar 23 '19 at 21:04
2

Could something like the following be useful?

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

Chris Dennett
  • 22,412
  • 8
  • 58
  • 84
2

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)

Johnydep
  • 6,027
  • 20
  • 57
  • 74
  • NEVER invoke "cmd" when calling an external program! This method is a breach of security! DO NOT USE! (parameters can be monitored with system tools) – marcolopes Dec 07 '22 at 12:23
1

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.

Syeda Zunaira
  • 5,191
  • 3
  • 38
  • 70
  • This method is a breach of security while calling external programs! DO NOT USE! (parameters can be monitored with system tools) – marcolopes Dec 07 '22 at 12:23
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);
    
Sasikumar Murugesan
  • 4,412
  • 10
  • 51
  • 74
1

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.

Mzzl
  • 3,926
  • 28
  • 39
1

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();
}
Joe Almore
  • 4,036
  • 9
  • 52
  • 77
-4

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