November 9, 2024
CMD / Java

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:

C:\Windows\system32\>java -jar <Path\to\your\program.jar> [Parameters...]

Example

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:\Windows\system32>cd C:\Example
C:\Example> java -jar Add.jar 4 2
 6 (This is the output of the JAR)
C:\Example>

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).

This article is also available in German on Computer-Masters.de

You may also be interested in the following articles:

Leave a Reply

Your email address will not be published. Required fields are marked *