Execute .jar file from a Java program
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();
Run a jar File from java program
Using ProcessBuilder(java.lang.ProcessBuilder) will solve your problem. Syntax is as follows -
ProcessBuilder pb = new ProcessBuilder("java", "-jar", "absolute path upto jar");
Process p = pb.start();
You can redirent input/output/error to/from files as follows
File commands = new File("absolute path to inputs file");
File dirOut = new File("absolute path to outputs file");
File dirErr = new File("absolute path to error file");
dirProcess.redirectInput(commands);
dirProcess.redirectOutput(dirOut);
dirProcess.redirectError(dirErr);
How to run a .jar file from within Java program?
Use an absolute path instead of a relative path, that should prevent the path not being found if you run from any working directory. Otherwise add it to your classpath as Nizil said.
To get the current user's name, use System.getProperty("user.name")
and concatenate into your path.
user = System.getProperty("user.name");
cmd = "java -jar C/Users/" + user + "/appdata/Roaming/<folder>/<file>.jar";
Runtime.getRuntime().exec(cmd);
Run class in Jar file
Use java -cp myjar.jar com.mypackage.myClass
.
If the class is not in a package then simply
java -cp myjar.jar myClass
.If you are not within the directory where
myJar.jar
is located, then you can do:On Unix or Linux platforms:
java -cp /location_of_jar/myjar.jar com.mypackage.myClass
On Windows:
java -cp c:\location_of_jar\myjar.jar com.mypackage.myClass
Execute jar file from inside a Java program
Just run it using
Runtime.getRuntime().exec("java", "-jar", yourJarName, parameter1, parameter2);
You can pass in an arbitrary number of parameters.
As Malt described below, if you need the output of the resulting process, you can use the following code:
Process p = Runtime.getRuntime().exec("java", "-jar", yourJarName, parameter1, parameter2);
InputStream is = p.getInputStream();
Note that you call InputStream
to get the output of the process, because you are reading from the stream, so it is an input with regards to your program, and an output with regards to the subprocess.
Run a JAR file from the command line and specify classpath
When you specify -jar
then the -cp
parameter will be ignored.
From the documentation:
When you use this option, the JAR file is the source of all user classes, and other user class path settings are ignored.
You also cannot "include" needed jar files into another jar file (you would need to extract their contents and put the .class files into your jar file)
You have two options:
- include all jar files from the
lib
directory into the manifest (you can use relative paths there) - Specify everything (including your jar) on the commandline using
-cp
:java -cp MyJar.jar:lib/* com.somepackage.subpackage.Main
Running JAR file on Windows
Easiest route is probably upgrading or re-installing the Java Runtime Environment (JRE).
Or this:
- Open the Windows Explorer, from the Tools select 'Folder Options...'
- Click the File Types tab, scroll down and select JAR File type.
- Press the Advanced button.
- In the Edit File Type dialog box, select open in Actions box and click Edit...
- Press the Browse button and navigate to the location the Java interpreter javaw.exe.
- In the Application used to perform action field, needs to display something similar to
C:\Program Files\Java\j2re1.4.2_04\bin\javaw.exe" -jar "%1" %
(Note: the part starting with 'javaw' must be exactly like that; the other part of the path name can vary depending on which version of Java you're using) then press the OK buttons until all the dialogs are closed.
Which was stolen from here: http://windowstipoftheday.blogspot.com/2005/10/setting-jar-file-association.html
Related Topics
Running Multiple Launch Configurations at Once
Code for Changing the Color of Subtasks in Gantt Chart
Named Placeholders in String Formatting
Why Java.Util.Optional Is Not Serializable, How to Serialize the Object with Such Fields
How to Do Union, Intersect, Difference and Reverse Data in Java
Variable's Scope in a Switch Case
Convert String to Calendar Object in Java
Httpservletrequest - How to Obtain the Referring Url
Java.Io.Ioexception: Mark/Reset Not Supported
Create a New Line in Java's Filewriter
Detecting Collision of Two Sprites That Can Rotate
In What Order Do Static Blocks and Initialization Blocks Execute When Using Inheritance
How to Use Jsch for Ssh Key-Based Communication
Java:Parse Java Source Code, Extract Methods
Do Hibernate Table Classes Need to Be Serializable
Compiler Error When Declaring a Variable Inside If Condition and No Curly Braces