SoFunction
Updated on 2025-03-01

Steps to run a jar program in the Linux background

Preparation

Before you begin, make sure that your Linux system has a Java running environment (JRE or JDK) installed.

Step 1: Create an executable jar file

First, make sure you have written an executable Java program and packaged it into a jar file. If you already have an executable jar file, you can skip this step.

$ cd /path/to/your/java/project
$ javac 
$ jar cvfe  YourProgram 

In the above code snippet, it is your Java program file, YourProgram is your main class name, and it is the compiled bytecode file. Executing these commands will create a jar file named.

Step 2: Run the jar program in the background

To run a jar program in the background, you can use the following command:

$ nohup java -jar  >  2>&1 &

In the above command, the nohup command is used to make the program ignore the SIGHUP signal and continue to run in the background. java -jar is the command to run the jar program. > Redirect the program's standard output to a file named. 2>&1 redirects the standard error output to the same place as the standard output. & is used to put commands into the background to execute. This way, your jar program will run in the background and write the output to the file. You can use the tail -f command to view the real-time log.

Step 3: View the background process

You can use the following command to view the currently running background process:

$ ps -ef | grep 

The above command lists all the included onesprocess. You can kill the specified process as needed,killThe command is as follows:

$ kill process_id

In the above command,process_idis the ID of the process you want to terminate. Through the above steps, you can successfully run a Java jar program background process in the Linux system.

Sample code to help understand:

#!/bin/bash
# Enter the directory where the jar program is locatedcd /path/to/your/jar/program
# Check if the program is runningif pgrep -f "" > /dev/null; then
  echo "The program is running in the background。"
else
  # Start the jar program  nohup java -jar  >  2>&1 & 
  echo "The program has been successfully started and run in the background。"
fi

In the above example code, we first enter the directory where the jar program is stored through the cd command. Then, use the pgrep command to check whether there is a background process with the same name running. If a process with the same name is already running, output a prompt message and end the execution of the script. If no process with the same name is running, we execute the nohup java -jar > 2>&1 & command to start the jar program and redirect the standard output and standard error output to the file. At the same time, use the nohup command to make the program ignore the SIGHUP signal and continue to run in the background. Save the above code as a shell script file (for example run_program.sh) and give execution permissions (chmod +x run_program.sh). You can then execute the script by running ./run_program.sh in your terminal. For this sample code, suppose your jar program file is named and stored in the /path/to/your/jar/program directory. The script will detect whether a background process with the same name is running, if not, start the program and write the output log to the file. Please note that absolute paths are used in the sample code, and you need to modify the path and file name according to the actual situation to adapt to your jar program.

jarIt is a command line tool in Java that is used to create, view and manipulate Java archive files (JAR files). JAR (Java Archive) files are a special compressed file format that can easily package and distribute classes, resource files, metadata and other content in Java programs.jarThe basic syntax of the command is as follows:

jar [Options] [jardocument] [输入document]

Here are some commonly used onesjarCommand options:

  • c: Create a new JAR file.
  • x: Unzip existing JAR files.
  • t: List the contents in the JAR file.
  • u: Update existing files in the JAR file.
  • v: Detailed output of operations, that is, display the detailed information of each file.
  • f: Specify the name of the JAR file. The following isjarSome common uses of commands:
  1. Create a JAR file:
jar cf  

The above command willCreate a file namedJAR file.

  • Unzip the JAR file:
jar xf 

The above command will decompress the nameJAR file to the current directory.

  • List the contents in the JAR file:
jar tf 

The above command will be listed asall files in the JAR file.

  • Add files to existing JAR files:
jar uf  

The above command will be namedAdd to JAR filedocument.

  • View details of JAR files:
jar tvf 

The above command will display the nameDetails of the JAR file, including permissions, size and modification date of each file.jarCommands can not only be used to package and decompress JAR files, but also to operate WAR files (Web Application Archive) and EAR files (Enterprise Application Archive). You can implement more complex operations by adding different options and parameters.

This is the article about the process steps of running jar programs in the Linux background. For more related content on running jar programs in the Linux background, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!