Windows: Run Java JAR on the command line
After compiling Java Source Code, you most likely get a JAR-file that needs to be run on the Java Virtual Machine (JVM). In some cases you need to run the JAR on the command line. Passing parameters on the command line is fairly easy and you cann insert your full command in your batch script.
Please keep in mind, that the Java Runtime Environment (JRE) needs to be installed on your machine and the JAVA_HOME Path must be set correctly in order to be able to run a JAR on the command line. When installing the JRE this should happen automatically.
Running a JAR on the command line
To run a JAR on the command line the command should look like the following:
Our Jar file is C:\Example\Add.jar
Our JAR is expecting two parameters (Integer) that will be added after running the JAR.
A call to the JAR can look like the following (using the Windows Change Directory cd command).
C:\Example> java -jar Add.jar 4 2
6 (This is the output of the JAR)
Java Virtual Machine JVM Parameters
Additionally you can add paramters to the Java Virtual Machine when running your JAR. To do so write the additional paramters between "java" and "-jar" and mark them with a "-". Parameters like this can be used for example for debugging.
You can find a list of possible JVM parameters in the Java Documentation (external link).
You still have questions?
Feel free to use our comment form that is also available for non-registered users.