Execute .jar file from a Java program
Asked Answered
C

10

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.

Cassidycassie answered 8/2, 2011 at 17:32 Comment(0)
V
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();
Verada answered 8/2, 2011 at 17:33 Comment(4)
@Verada Can I integrate the System.out in the original program? excuse the question but I'm new in javaCassidycassie
Not sure I understand your comment, but you get a Process back from start() which provides you with getInputStream / getOutputStream.Verada
Sorry again .. and if I wanted to pass a parameter to the jar as do I?Cassidycassie
You just add more arguments to the ProcessBulider constructor: new ProcessBuilder("/path/to/java", "-jar", "your.jar", "arg1", "arg2");Verada
C
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));
Crosley answered 27/10, 2013 at 4:53 Comment(1)
Please use System.err.println(new String(c)); for printing the error messages.Godsend
Z
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.

Zacheryzack answered 8/2, 2011 at 17:40 Comment(4)
Please drag yourself (kicking and screaming - if need be) into the abilities of Java 1.5 & ProcessBuilder.Coral
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.Overthecounter
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: javaworld.com/article/3176874/java-language/…. There is already an article that discusses the difference between Runtime and ProcessBuilder: #6856528.Zacheryzack
This approach won't work if you need to specify working directory. ProcessBuilder helped me (as suggested in the answer above).Pedant
P
2

Could something like the following be useful?

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

Personify answered 8/2, 2011 at 18:0 Comment(0)
S
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)

Sherris answered 3/2, 2012 at 10:31 Comment(1)
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)Witherite
G
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.

Goldin answered 12/2, 2015 at 5:35 Comment(1)
This method is a breach of security while calling external programs! DO NOT USE! (parameters can be monitored with system tools)Witherite
C
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);
    
Crankcase answered 7/7, 2017 at 0:25 Comment(1)
This one is a great idea! I wonder if it works with RUNNABLE JARs :DWitherite
D
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.

Dorcia answered 8/3, 2019 at 18:16 Comment(0)
C
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();
}
Coventry answered 12/4, 2022 at 16:42 Comment(0)
S
-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

Spiritualism answered 17/1, 2013 at 14:4 Comment(1)
The question is how to run the jar file from a program, not how to do it from the command line.Bodwell

© 2022 - 2024 — McMap. All rights reserved.