SoFunction
Updated on 2025-04-21

Detailed explanation of java-jar command: Running JAR files, passing parameters and performance tuning

Preface

The java -jar command is a way to run executable JAR (Java Archive) files in Java development.

JAR files are file formats used to package multiple Java class files, related metadata and resources (such as text, pictures, etc.) into one file for easy distribution, deployment and version control.

The following is a detailed introduction to the relevant content of the java -jar command.

1. Basic usage

The basic syntax of the java -jar command is as follows:

java -jar

Among them, is the name of the JAR file you want to run.

When running this command, the Java virtual machine (JVM) will look for files in the JAR file, which is located in the META-INF directory of the JAR file.

The file specifies the metadata of the JAR file, including the main class (Main-Class) information. The JVM will load and execute the main method of this main class.

2. Things to note

  • Make sure the JAR file is executable: the JAR file must contain a main class and the main class is correctly declared in the file.

  • Environment variables: Make sure your system has Java Development Kit (JDK) or Java Runtime Environment (JRE) installed, and the JAVA_HOME environment variable is set correctly, and %JAVA_HOME%\bin (Windows) or $JAVA_HOME/bin (Unix/Linux/macOS) has been added to the system's PATH environment variable.

  • Dependency management: If the JAR file depends on other libraries or JAR packages, you need to use the Class-Path attribute to specify the paths for these dependencies in the file, or use the -cp or -classpath parameters to specify them at runtime.

3. Advanced usage

The java -jar command also supports some advanced usages, such as passing parameters to the main class in the JAR file. These parameters are directly followed by the JAR file name, for example:

java -jar  param1 param2

These parameters are passed to the main method of the main class.

4. Common parameters

In addition to running JAR files directly, java commands also support some parameters to optimize the performance of the JVM or configure system properties. These parameters can be used with the -jar option, for example:

• -Xms: Set the initial size of the Java heap.

• -Xmx: Set the maximum size of the Java heap.

• -Xss: Sets the stack size of each thread.

• -D=: Set system properties.

• -verbose:class: Print class loading information.

• -verbose:gc: Print garbage collection information.

For example, to start a JAR file on the foreground mode and set the maximum heap memory to 2G, you can use the following command:

java -Xmx2G -jar 

5. Background operation and logging

If you want the JAR file to run in the background and want to redirect the output to the file, you can use the nohup command (Linux/macOS) or the start/B command (Windows). For example, in Linux, you can use the following command:

nohup java -jar  >  2>&1 &

This command will let the JAR file run in the background and redirect both standard output and standard errors to the file.

Example Introduction

Run an executable JAR file named

Suppose you have a JAR file named, which contains a main class HelloWorld, which has a main method for outputting the "Hello, World!" string. Now, you want to run this JAR file through the java -jar command.

Step 1: Prepare the JAR file

First, make sure you already have the file and that the file is executable. This means that the JAR file must contain a META-INF/ file that specifies the main class (Main-Class) as HelloWorld.

Step 2: Open the command line tool

Open your command line tool (CMD or PowerShell on Windows, Terminal on macOS or Linux).

Step 3: Switch to the directory where the JAR file is located

Use the cd command to switch to the directory containing the file. For example, if the JAR file is located in the C:\Users\YourName\Desktop directory (Windows example), enter:

cd C:\Users\YourName\Desktop

Or, if the JAR file is in the /home/yourname/Desktop directory (Linux/macOS example), enter:

cd /home/yourname/Desktop

Step 4: Run the JAR file

Enter the following command in the command line to run the JAR file:

java -jar 

After pressing Enter, you should see the output "Hello, World!" string in the command line window.

Additional Notes:

• Backstage Run: If you want the JAR file to run in the background and don't want it to occupy the current command line window, you can do this using specific commands or tools from the operating system. For example, on Linux/macOS, you can use the nohup command or the & symbol to put the command in the background to execute.

nohup java -jar  >  2>&1 &

This command redirects the output to the file, and the JAR file will continue to run in the background even if you close the terminal.

• Logging: As shown in the above example, you can record the run log of the JAR file by redirecting the output to the file.

• Pass parameters: If your JAR file needs to receive command line parameters, you can add them directly after the java -jar command. These parameters are passed to the main method of the main class in the JAR file.

java -jar  arg1 arg2

• Performance Tuning: You can optimize the running performance of JAR files through JVM parameters. For example, use the -Xmx and -Xms parameters to set the maximum and initial size of the JVM heap.

java -Xmx512m -Xms256m -jar 

The above is a detailed explanation of the use of the java -jar command through examples. Hope this helps you!

summary

java -jar command is a standard way to run executable JAR files in Java development.

By using this command and its parameters reasonably, you can easily deploy and run Java applications.

At the same time, understanding the structure of JAR files and the role of files is also very important for a deep understanding of the packaging and distribution of Java applications.

This is the article about running JAR files, passing parameters and performance tuning in detail in java -jar command. This is the end of this article. For more related java -jar command running JAR files, passing parameters, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!