Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 735
vfgjrk Avatar asked Feb 08 '11 17:02

vfgjrk


People also ask

Can you execute a JAR file?

Most JAR files are simply containers for data that another program needs to run with Java; therefore you cannot run these files and nothing will happen when you double-click them. Similarly, most executable JAR files are downloaded as installation files to install applications or programs.

How do I open a JAR file with JDK?

Right-click on the JAR file. Go to “Open With Other Applications”. Select Show other applications. Select Open With OpenJDK Java X Runtime.


2 Answers

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(); 
like image 73
aioobe Avatar answered Sep 21 '22 07:09

aioobe


    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)); 
like image 40
Anderson Lopes Avatar answered Sep 22 '22 07:09

Anderson Lopes