Environment Requirements
Four Ways to Deploy Java Services on Linux (Including Environment Dependencies and Start/Stop Instructions)
This article provides four methods for deploying Java services, compatible with JDK 21, detailing the environment dependencies, start, stop, and restart operations for each method.
The JAR file provided in this example has been automatically placed in the /root/resume/ directory, with the filename: resume-core-1.0-SNAPSHOT.jar
Verification address after starting the service: http://your_server_host:8192Devops
Command to start the target service (detailed explanation of command parameters will be provided in the appendix at the end of this article):
cd /root/resume/ls -ltr resume-core-1.0-SNAPSHOT.jarjava -jar \ -Ddb.host=your_db_host \ -Ddb.port=3306 \ -Ddb.name=guoya \ -Ddb.username=root \ -Ddb.password=com.guoyasoft \ -Dserver.port=8192 \ /root/resume/resume-core-1.0-SNAPSHOT.jar
1. Method One: Start Using the <span>java -jar</span>
Command Directly
Environment Dependencies
-
Operating System: Linux (Ubuntu/CentOS, etc.).
-
Java Environment: JDK 21 must be installed (consistent with the JAR package compilation version).
-
File Path: The JAR package must be uploaded to the server (e.g.,
<span>/root/resume/</span>
). -
Port Occupation: Ensure that port
<span>8192</span>
is not occupied. -
Network Configuration: The server must be able to access the database service (e.g., MySQL’s
<span>3306</span>
port).
Start Service
(Before executing the command, replace your_db_host with the IP address of your MySQL database in your environment)
nohup java -jar \ -Ddb.host=your_db_host \ -Ddb.port=3306 \ -Ddb.name=guoya \ -Ddb.username=root \ -Ddb.password=com.guoyasoft \ -Dserver.port=8192 \ /root/resume/resume-core-1.0-SNAPSHOT.jar > /root/resume/app.log 2>&1 &&
Stop Service
-
Find Process ID:
pgrep -f "resume-core-1.0-SNAPSHOT.jar"
-
Terminate Process:
kill <PID> # Replace <PID> with the actual process number
-
Force Terminate Process:
kill -9 <PID> # Replace <PID> with the actual process number
Restart Service
-
Stop the service (refer to the steps above).
-
Re-run the start command.
2. Method Two: Use Shell Script to Wrap Start Script
Environment Dependencies
-
Operating System: Linux (supports Bash).
-
Java Environment: JDK 21 must be installed.
-
Script Permissions: The script file must have executable permissions.
-
Tool Dependencies: Must pre-install
<span>nohup</span>
(for background running) and<span>pgrep</span>
(for process management). -
Log Path: Ensure the directory where the script is located has write permissions (to generate
<span>app.log</span>
).
Script Example <span>run.sh</span>
(Before executing the command, replace your_db_host with the IP address of your MySQL database in your environment)
cd /root/resume/cat > run.sh <<EOF#!/bin/bashJAR_PATH="/root/resume/resume-core-1.0-SNAPSHOT.jar"DB_HOST="your_db_host"DB_PASSWORD="com.guoyasoft"start() { nohup java -jar \ -Ddb.host=\