JAR Deployment
Java JAR deployment means copying an executable JAR to a server and running it with java -jar so it keeps working after you log out, restarts cleanly and can be updated or rolled back safely. It is the simplest way to put a Spring Boot app or any self-contained Java service into production. All you need on the server is a Java runtime, a folder layout and a small start/stop script.
This lesson deploys the CourseServer app from the Java deployment lesson to a Linux server (Ubuntu here) step by step. Every command and output was tested with JDK 21.
What a Java JAR Deployment Needs
| Piece | Example | Purpose |
|---|---|---|
| Executable JAR | course-server-1.1.0.jar | Your application with a Main-Class |
| Java runtime | OpenJDK 21 or a jlink runtime | Runs the JAR |
| Dedicated user | courseapp | The app does not run as root |
| App folder | /opt/course-server | Releases, config, logs and scripts |
| Config file | app.env | Port, environment name, secrets |
| Start/stop script | app.sh | Starts in the background, stops cleanly |
How it works:
- The JAR must run on its own with
java -jar. If it needs extra libraries, build a fat JAR first, as shown in Build executable JAR in Java. Spring Boot's Maven and Gradle plugins do this automatically. - Everything else on this list is set up once per server. Later deployments only copy a new JAR.
Prepare the Server for JAR Deployment
Install Java, create a system user and create the folder layout.
# on the server
sudo apt update
sudo apt install -y openjdk-21-jre-headless
java -version
sudo useradd --system --create-home --home-dir /opt/course-server --shell /usr/sbin/nologin courseapp
sudo -u courseapp mkdir -p /opt/course-server/{releases,logs}
ls -ld /opt/course-server /opt/course-server/*
openjdk version "21.0.12" 2026-07-21
OpenJDK Runtime Environment (build 21.0.12+7-Ubuntu-1ubuntu224.04)
OpenJDK 64-Bit Server VM (build 21.0.12+7-Ubuntu-1ubuntu224.04, mixed mode, sharing)
drwxr-x--- 4 courseapp courseapp 4096 Nov 18 10:20 /opt/course-server
drwxr-xr-x 2 courseapp courseapp 4096 Nov 18 10:20 /opt/course-server/logs
drwxr-xr-x 2 courseapp courseapp 4096 Nov 18 10:20 /opt/course-server/releases
How it works:
- The
-jre-headlesspackage is enough to run Java; a server does not need the compiler or GUI libraries. courseappis a system user that cannot log in (nologin). If the app has a security hole, an attacker gets only this user's limited rights, not root.- The
{releases,logs}braces create both folders in one command.
Note: Your exact
java -versionbuild string depends on the distribution's update. What matters is the major version (21 here) matching what you tested with.
Copy the JAR to the Server
Copy the JAR from your machine (or CI server) with scp, then move it into a versioned release folder.
# on your machine
scp target/course-server-1.1.0.jar asha@203.0.113.10:/tmp/
# on the server
sudo -u courseapp mkdir -p /opt/course-server/releases/1.1.0
sudo cp /tmp/course-server-1.1.0.jar /opt/course-server/releases/1.1.0/app.jar
sudo chown courseapp:courseapp /opt/course-server/releases/1.1.0/app.jar
sudo -u courseapp ln -sfn /opt/course-server/releases/1.1.0 /opt/course-server/current
ls -l /opt/course-server/current
lrwxrwxrwx 1 courseapp courseapp 34 Nov 18 10:22 /opt/course-server/current -> /opt/course-server/releases/1.1.0
How it works:
- Each version has its own folder, and the JAR inside is always called
app.jar. Scripts only ever refer tocurrent/app.jar. currentis a symbolic link. Switching versions means changing one link, and the previous release is still on disk for a fast rollback.203.0.113.10is an example address; use your server's IP or host name.
Run the JAR in the Foreground
First run the app by hand to see that it starts.
sudo -u courseapp APP_PORT=8080 APP_ENV=prod java -Xmx256m -jar /opt/course-server/current/app.jar
CourseServer 1.1.0 started on port 8080 (env=prod, java=21)
How it works:
sudo -u courseappruns the JVM as the app user, exactly as it will run later.- The process is attached to your terminal. Press Ctrl+C or close the SSH session and the app stops. This is fine for a test, but not for production.
Keep the JAR Running With nohup
nohup starts the process so it survives logout, and & puts it in the background. Output goes to a log file.
cd /opt/course-server
sudo -u courseapp bash -c 'APP_PORT=8080 APP_ENV=prod nohup java -Xmx256m -jar current/app.jar >> logs/app.log 2>&1 & echo $! > app.pid'
cat app.pid
tail -n 1 logs/app.log
curl -s -w "\n" http://localhost:8080/health
805
CourseServer 1.1.0 started on port 8080 (env=prod, java=21)
{"status":"UP","version":"1.1.0","env":"prod","uptimeSeconds":1}
How it works:
>> logs/app.log 2>&1appends both normal output and errors to one log file.$!is the process ID of the last background command. Saving it inapp.pidlets you stop exactly this process later withkill $(cat app.pid).- Typing this line by hand every time is error-prone, so the next section wraps it in a script.
Externalize Configuration With an Env File
Keep all settings in one file next to the app instead of on the command line.
# /opt/course-server/app.env (owned by courseapp, mode 600)
APP_PORT=8080
APP_ENV=prod
DB_PASSWORD=change-me
sudo chmod 600 /opt/course-server/app.env
ls -l /opt/course-server/app.env
-rw------- 1 courseapp courseapp 45 Nov 18 10:25 /opt/course-server/app.env
How it works:
- Mode
600means onlycourseappcan read the file, so other users on the server cannot see the password. - The same JAR goes to test and production; only
app.envdiffers between the servers. - A Spring Boot JAR reads these variables automatically:
APP_ENVmaps to the propertyapp.env, andSERVER_PORTsets the port. You can also use--spring.config.additional-location=file:/opt/course-server/config/for a properties file.
A Start/Stop Script for Java JAR Deployment
This script starts, stops, restarts and checks the app. It reads app.env, writes a PID file and gives the JVM time to shut down cleanly.
#!/usr/bin/env bash
# app.sh - start, stop and check the course server
APP_HOME="${APP_HOME:-/opt/course-server}"
JAR="$APP_HOME/current/app.jar"
PID_FILE="$APP_HOME/app.pid"
LOG_FILE="$APP_HOME/logs/app.log"
JAVA_OPTS="${JAVA_OPTS:--Xms128m -Xmx256m}"
# load settings such as APP_PORT and APP_ENV
[ -f "$APP_HOME/app.env" ] && set -a && . "$APP_HOME/app.env" && set +a
is_running() {
[ -f "$PID_FILE" ] && kill -0 "$(cat "$PID_FILE")" 2>/dev/null
}
case "$1" in
start)
if is_running; then echo "Already running (pid $(cat "$PID_FILE"))"; exit 0; fi
mkdir -p "$APP_HOME/logs"
nohup java $JAVA_OPTS -jar "$JAR" >> "$LOG_FILE" 2>&1 &
echo $! > "$PID_FILE"
echo "Started (pid $(cat "$PID_FILE"))"
;;
stop)
if ! is_running; then echo "Not running"; rm -f "$PID_FILE"; exit 0; fi
PID=$(cat "$PID_FILE")
kill "$PID" # SIGTERM: shutdown hook runs
for i in $(seq 1 20); do
is_running || { rm -f "$PID_FILE"; echo "Stopped"; exit 0; }
sleep 0.5
done
echo "Still running, forcing stop"; kill -9 "$PID"; rm -f "$PID_FILE"
;;
restart)
"$0" stop
"$0" start
;;
status)
if is_running; then echo "Running (pid $(cat "$PID_FILE"))"; else echo "Stopped"; exit 3; fi
;;
*)
echo "Usage: $0 {start|stop|restart|status}"; exit 1
;;
esac
cd /opt/course-server
sudo -u courseapp ./app.sh start
sudo -u courseapp ./app.sh start
sudo -u courseapp ./app.sh status
ps -o pid,args -p $(cat app.pid)
sudo -u courseapp ./app.sh restart
sudo -u courseapp ./app.sh stop
sudo -u courseapp ./app.sh status; echo "exit code: $?"
cat logs/app.log
Started (pid 805)
Already running (pid 805)
Running (pid 805)
PID COMMAND
805 java -Xms128m -Xmx256m -jar /opt/course-server/current/app.jar
Stopped
Started (pid 854)
Stopped
Stopped
exit code: 3
CourseServer 1.1.0 started on port 8080 (env=prod, java=21)
Shutting down...
Stopped.
CourseServer 1.1.0 started on port 8080 (env=prod, java=21)
Shutting down...
Stopped.
How it works:
set -aexports every variable loaded fromapp.env, so the JVM sees them as environment variables.kill -0sends no signal; it only checks whether the process still exists. That makesis_runningsafe to call any time.stopsends SIGTERM first, which runs the app's shutdown hook ("Shutting down... Stopped." in the log). Only if the app is still alive after 10 seconds does it usekill -9, which cannot be caught.statusreturns exit code 3 when stopped, the same convention service managers use, so other scripts can test it.
Tune JVM Options for Production
Pass JVM options through JAVA_OPTS without editing the script.
| Option | Effect |
|---|---|
-Xms128m -Xmx256m | Starting and maximum heap size |
-XX:MaxRAMPercentage=75 | Max heap as a share of available memory (good in containers) |
-XX:+ExitOnOutOfMemoryError | Exit instead of limping on after an out-of-memory error |
-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=logs/ | Save a heap dump for later analysis |
-Duser.timezone=Asia/Kolkata | Fixed time zone, independent of server settings |
-Dfile.encoding=UTF-8 | Explicit text encoding (already the default since JDK 18) |
# /opt/course-server/app.env
JAVA_OPTS="-Xmx256m -XX:+ExitOnOutOfMemoryError -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/opt/course-server/logs"
java -XX:+PrintFlagsFinal -version 2>/dev/null | grep -E " ExitOnOutOfMemoryError| HeapDumpOnOutOfMemoryError"
bool ExitOnOutOfMemoryError = false {product} {default}
bool HeapDumpOnOutOfMemoryError = false {manageable} {default}
How it works:
- Because
app.shloadsapp.envbefore readingJAVA_OPTS, putting the line inapp.envoverrides the script's default. -XX:+PrintFlagsFinallists every JVM flag and its current value. Both flags arefalseby default, which is why you turn them on explicitly.ExitOnOutOfMemoryErrormakes the process exit so a script or service manager can restart it, instead of leaving a broken app running. The JVM options lesson covers these flags in more depth.
Note: Ports below 1024, such as 80 and 443, need root rights. Keep the Java app on a high port like 8080 and put a reverse proxy such as Nginx in front of it to handle port 443 and TLS.
Roll Back a JAR Deployment
Because old releases stay in releases/, a rollback is one link change and a restart.
cd /opt/course-server
ls releases
readlink current
sudo -u courseapp ln -sfn /opt/course-server/releases/1.0.0 current
sudo -u courseapp ./app.sh restart
sleep 2
curl -s -w "\n" http://localhost:8080/health
1.0.0
1.1.0
/opt/course-server/releases/1.1.0
Stopped
Started (pid 912)
{"status":"UP","version":"1.0.0","env":"prod","uptimeSeconds":1}
How it works:
readlink currentshows which release is live before you change anything.- After the restart,
/healthreports version 1.0.0, proving the old release is serving requests. - Delete old release folders from time to time, but always keep at least the previous one.
Real-World Example: One-Command JAR Deployment From Your Laptop
This script runs on your machine or in CI. It builds the JAR, uploads it, installs it as a new release on the server, switches current, restarts the app and checks health, rolling back automatically if the new version does not come up. It uses the deploy.sh script from the Java deployment lesson, stored in /opt/course-server on the server.
#!/usr/bin/env bash
# release.sh <version> - build locally and deploy to the server
set -euo pipefail
VERSION="$1"
SERVER="asha@203.0.113.10"
JAR="target/course-server-$VERSION.jar"
echo "==> Building $VERSION"
mvn -q -DskipTests versions:set -DnewVersion="$VERSION" -DgenerateBackupPoms=false
mvn -q -DskipTests clean package
ls -lh "$JAR" | awk '{print " " $5, $9}'
echo "==> Uploading"
scp -q "$JAR" "$SERVER:/tmp/app-$VERSION.jar"
echo "==> Deploying on server"
ssh "$SERVER" "sudo -u courseapp APP_PORT=8080 /opt/course-server/deploy.sh /tmp/app-$VERSION.jar $VERSION \
&& rm /tmp/app-$VERSION.jar"
echo "==> Done: $VERSION is live"
./release.sh 1.1.0
==> Building 1.1.0
3.1K target/course-server-1.1.0.jar
==> Uploading
==> Deploying on server
==> Installing 1.1.0
==> current -> releases/1.1.0
Stopped
Started (pid 515)
==> Waiting for health check
==> Healthy: {"status":"UP","version":"1.1.0","env":"prod","uptimeSeconds":0}
==> Done: 1.1.0 is live
A broken build stops the release and leaves the old version running:
./release.sh 1.2.0
==> Building 1.2.0
3.1K target/course-server-1.2.0.jar
==> Uploading
==> Deploying on server
==> Installing 1.2.0
==> current -> releases/1.2.0
Stopped
Started (pid 626)
==> Waiting for health check
==> Health check failed, rolling back
Not running
Started (pid 694)
==> Rolled back to 1.1.0
How it works:
- Versioned build.
versions:setwrites the version intopom.xml, so the JAR name and its manifestImplementation-Versionmatch the release number the health check reports. - Upload then install. The JAR lands in
/tmpfirst and is copied intoreleases/<version>/app.jarbydeploy.sh, running ascourseapp, so file ownership is always correct. - Fail fast.
set -euo pipefailstopsrelease.shat the first failing command. Whendeploy.shexits with code 1 after a rollback, thesshcommand fails, "Done" is never printed, and a CI pipeline marks the job as failed. - No downtime surprises. The old process stops with SIGTERM and finishes open requests before the new one starts. The gap is a second or two; running two instances behind a load balancer removes even that.
- Next step.
app.shandnohupdo not restart the app after a server reboot or crash. The next lesson, Run as Service, hands the same JAR tosystemdso Linux manages it for you.