Learning how to run a jar file in linux is an essential skill for developers, system administrators, and anyone working with Java-based applications on open-source systems. Now, while Linux does not execute JAR files natively like a standard binary, the Java Runtime Environment (JRE) or Java Development Kit (JDK) provides the tools needed to launch them. A JAR (Java Archive) file packages compiled Java code, resources, and metadata into a single distributable file, making it easy to deploy programs across different environments. This guide walks you through every step, from installing Java to troubleshooting common errors, so you can confidently run a jar file in linux on any distribution That's the part that actually makes a difference..
What Is a JAR File?
A JAR file is essentially a ZIP archive with a .In real terms, jar extension that contains Java class files, a manifest file, and optional resources such as images or configuration files. The manifest file, located at META-INF/MANIFEST.MF, tells the Java Virtual Machine (JVM) which class contains the main method to execute. Without a properly configured manifest, you cannot run the file using the standard java -jar command, though you can still execute specific classes manually.
Prerequisites: Installing and Checking Java on Linux
Before you can run a jar file in linux, you must see to it that Java is installed on your system. Most Linux distributions do not include Java by default, so you may need to install it manually That's the part that actually makes a difference..
Check if Java Is Installed
Open your terminal and run:
java -version
If Java is installed, you will see output similar to:
openjdk version "17.0.8" 2023-07-18
OpenJDK Runtime Environment (build 17.0.8+7-Debian-1deb12u1)
OpenJDK 64-Bit Server VM (build 17.0.8+7-Debian-1deb12u1, mixed mode, sharing)
If the terminal returns command not found, you need to install Java But it adds up..
Install Java Using Your Package Manager
On Debian or Ubuntu-based systems:
sudo apt update
sudo apt install default-jre
On RHEL, CentOS, or Fedora:
sudo dnf install java-17-openjdk
Or on older versions using Yum:
sudo yum install java-11-openjdk
After installation, verify it again with java -version. You only need the JRE to run applications, but developers often install the full JDK for compilation and debugging.
How to Run a JAR File in Linux (Step-by-Step)
Once Java is installed, running a JAR file is straightforward. The most common method uses the java command with the -jar flag No workaround needed..
Method 1: Using the java -jar Command
deal with to the directory containing your JAR file using the cd command, then run:
java -jar application.jar
Replace application.jar with the actual name of your file. If the JAR has a valid manifest with a Main-Class entry, the JVM will launch the application immediately.
Method 2: Running an Executable JAR Directly
Some JAR files are marked as executable. You can make a JAR executable with:
chmod +x application.jar
Then run it directly:
./application.jar
This method only works if the JAR includes a proper shebang or if your system is configured to associate .jar files with the Java launcher. For most users, java -jar remains the more reliable approach Still holds up..
Passing Command-Line Arguments
Many Java applications accept arguments when launched. You can pass them after the JAR filename:
java -jar application.jar --port 8080 --config production.yml
The application receives these arguments in its main(String[] args) method, allowing you to control behavior without editing the code.
Running a JAR File in the Background
If you are running a server or a long-running process, you probably do not want it to stop when you close the terminal. Linux offers several ways to keep a JAR running in the background But it adds up..
Using nohup and &
The nohup command ignores hangup signals, and the & symbol
runs the process in the background, freeing your terminal for other commands.
nohup java -jar application.jar > server.log 2>&1 &
Here, nohup prevents the process from terminating when you log out, > redirects standard output to server.By default, nohupwrites output tonohup.log, 2>&1redirects errors to the same file, and the trailing& detaches the process from your shell. out if you do not specify a file.
To verify the application is running, use:
pgrep -f application.jar
or
ps aux | grep java
If you need to stop the background process, find its process ID (PID) and terminate it:
## Stopping a Background JAR Process
The moment you launch a JAR file in the background with **nohup** (or `&`), the terminal stays open while the program runs. To cleanly shut it down later, locate the process that was started:
```bash
# Show all java‑related processes
ps aux | grep java
# Or, more directly, match the exact jar name
pgrep -f application.jar
The output will contain lines like:
user 12345 0.0 12.3 /usr/bin/java -jar application.jar
Take note of the PID (Process ID) shown in the fourth column. You can now terminate the job in several ways:
Graceful termination
Send a SIGTERM signal – this lets the JVM finish writing to the log before exiting:
kill 12345
Forceful termination (last resort)
If the JVM does not respond, send SIGKILL:
kill -9 12345
Terminating by process name
Because the pattern -f application.jar may appear even when multiple instances are running, it’s safer to supply the numeric PID found above rather than relying solely on the filename.
Quick checklist for stopping a detached JAR
| Step | Command |
|---|---|
| Identify PID | pgrep -f application.jar (or ps aux | grep java) |
| Send TERM | kill <PID> |
| Force KILL (if needed) | kill -9 <PID> |
| Verify removal | pgrep -f application.jar → should return nothing |
For services managed by systemd, you can also disable and start them non‑interactively:
sudo systemctl stop java-app.service # stops the service containing your JAR
sudo systemctl restart java-app.service # restarts it
Summary & Best Practices
- Installation – After adding
java-11-openjdk, confirm the JRE withjava -version. A full JDK is optional unless you plan to compile or debug. - Executing a JAR – Prefer
java -jar application.jar. For executables,chmod +x application.jar && ./application.jarworks only under specific configurations. - Passing arguments – Append flags right after the JAR name (
--port 8080 --config production.yml). They becomemain(String[] args)parameters. - Background execution – Use
nohup java -jar application.jar > server.log 2>&1 &to keep the app alive after logout. Redirect stdout/stderr to a log file for auditability. - Monitoring – Check the log file (
server.log) and view live output withtail -f server.logorjournalctl -u java-app. - Stopping cleanly – Locate the PID via `
Stopping cleanly – locate the PID
Once you have the numeric identifier (the fourth column from the ps aux | grep java output), you can issue the appropriate signal. If the Java Virtual Machine is still alive when you send SIGTERM, it will give it a chance to flush its buffers and close files before disappearing:
Counterintuitive, but true.
kill 12345 # SIGTERM
If the process does not respond within a few seconds, escalate to the more aggressive SIGKILL:
kill -9 12345 # SIGKILL
Verifying removal
After either command, confirm that the entry no longer appears:
pgrep -f application.jar # or simply `ps aux | grep java`
A clean exit line (or an empty result) indicates the job has been fully terminated.
Using systemd for managed Java applications
Many production environments run the JAR behind a systemd service unit. Stopping such a service handles both the JAR and any auxiliary daemons automatically:
sudo systemctl stop java-app.service # halts the whole service
sudo systemctl restart java-app.service # brings it back up
When you launch a service manually, you can achieve the same effect:
sudo systemctl start java-app.service # starts the detached instance
Systemd’s “restart” option also lets you define RestartSec= or Restart=on-failure to make restarts smoother without manual intervention That alone is useful..
Additional Tips for strong Background Execution
-
Logging – Redirect stdout/stderr to a dedicated file so you can audit activity later:
nohup java -jar application.jar > server.log 2>&1 &Rotate the log regularly (e.g.,
logrotate) to prevent disk‑space buildup. -
Environment variables – Set required variables before launching, e.g.:
export JAVA_OPTS="-Xms256m -Xmx512m"This ensures consistent memory sizing across sessions That's the whole idea..
-
Multiple instances – When several PIDs share the same name, always target the exact ID to avoid unintentionally killing unrelated programs Most people skip this — try not to..
-
Signal handling – Some applications ignore
SIGTERM. If this happens, fall back tokill -9immediately. -
Health checks – Periodically poll
server.logfor error patterns or use tools likesystemd-cgtopto verify the service stays up.
Summary & Best Practices
| Area | Recommendation |
|---|---|
| Installation | Verify JDK/JRE version with java -version. That said, yml`). |
| Background mode | Prefer nohup java -jar … > log.Avoid treating .Full JDK optional unless compilation/debug needed. txt 2>&1 &` to keep the process alive after logout. |
| Argument passing | Append flags right after the filename (--port 8080 --config prod.jar. Worth adding: jar as an executable script. log) or query live output through journalctl -u java-app. Confirm disappearance with pgrep. Log to a permanent file for traceability. That's why |
| Monitoring | Tail the log (`tail -f server. |
| Clean shutdown | Identify PID → kill <PID> (SIGTERM) → kill -9 <PID> only if necessary. On top of that, |
| Running a JAR | Always invoke it via java -jar application. They become parameters of main(String[] args)`. |
| Service management | For systemd units, use systemctl stop/start/restart rather than raw kill. |
By following these steps—capturing the PID early, sending a polite