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.
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.
Right-click on the JAR file. Go to “Open With Other Applications”. Select Show other applications. Select Open With OpenJDK Java X Runtime.
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();
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));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With