How To Run Jar File In Linux : Install Java Runtime Environment

Running EXE files on a Chromebook without Linux is possible using online converters or Chrome apps. But if you need to work with Java applications, you will likely encounter JAR files. Understanding how to run jar file in linux is a fundamental skill for developers and system administrators. This guide walks you through every step, from installation to troubleshooting.

How To Run Jar File In Linux

JAR stands for Java ARchive. It packages multiple Java class files and resources into one file. Think of it like a ZIP file for Java programs. To run one, you need the Java Runtime Environment (JRE) installed.

Most Linux distributions don’t include Java by default. You have to install it manually. Don’t worry, it’s simple.

Check If Java Is Already Installed

Open your terminal. Press Ctrl+Alt+T or search for “Terminal” in your apps menu.

Type this command and press Enter:

java -version

If you see version information, Java is installed. If you get a “command not found” error, you need to install it.

Install Java On Ubuntu Or Debian

Update your package list first:

sudo apt update

Then install the default JRE:

sudo apt install default-jre

This installs OpenJDK, which is the open-source version of Java. It works perfectly for most JAR files.

For Fedora, CentOS, or RHEL, use:

sudo dnf install java-11-openjdk

For Arch Linux:

sudo pacman -S jre-openjdk

Verify The Installation

Run the version check again:

java -version

You should see output like “openjdk version 11.0.18” or similar. Now you are ready to run JAR files.

Basic Command To Run A JAR File

The standard command is:

java -jar filename.jar

Replace “filename.jar” with your actual file name. Make sure you are in the same directory as the JAR file, or provide the full path.

For example, if your file is in Downloads:

java -jar ~/Downloads/myapp.jar

If the JAR file has a main class defined in its manifest, this command will launch the application. If not, you will get an error.

Running A JAR File With A Specific Main Class

Sometimes the manifest is missing or corrupted. You can specify the main class manually:

java -cp myapp.jar com.example.MainClass

Replace “com.example.MainClass” with the actual package and class name. You can find this inside the JAR file using:

jar tf myapp.jar

This lists all files inside the archive. Look for .class files that contain “main” in their name.

Running A JAR File In The Background

If the application runs continuously, you might want it in the background. Use the ampersand symbol:

java -jar myapp.jar &

This returns control to the terminal. To bring it back to the foreground, use the fg command.

For long-running server applications, consider using nohup:

nohup java -jar myapp.jar &

This keeps the process running even after you close the terminal.

Common Errors And Fixes

Even with Java installed, you might hit issues. Here are the most frequent problems.

“No Main Manifest Attribute” Error

This means the JAR file does not have a Main-Class entry in its manifest. You can fix this by creating a manifest file manually.

Create a text file named MANIFEST.MF with this content:

Main-Class: com.example.MainClass

Then repackage the JAR:

jar cfm myapp.jar MANIFEST.MF -C . .

Or use a tool like unzip and zip to modify the existing archive.

“Java Is Not Recognized” Error

This usually means Java is not in your PATH. Check if it is installed correctly. If it is, add it to your PATH:

export PATH=$PATH:/usr/lib/jvm/java-11-openjdk/bin

Add this line to your .bashrc or .zshrc file to make it permanent.

Permission Denied

Make sure the JAR file has execute permissions:

chmod +x myapp.jar

Then try running it again.

Outdated Java Version

Some JAR files require a specific Java version. Check the application’s documentation. You can install multiple Java versions and switch between them using update-alternatives:

sudo update-alternatives --config java

How To Run A JAR File As An Executable

You can make a JAR file double-clickable in your file manager. First, ensure it has execute permissions:

chmod +x myapp.jar

Then associate JAR files with Java. On GNOME, right-click the file, go to Properties, then Open With, and select “OpenJDK Java Runtime.”

On KDE, right-click, select Properties, then File Type Options, and add Java as the default application.

Alternatively, create a desktop shortcut. Create a file ending in .desktop with content like:

[Desktop Entry]
Type=Application
Name=My App
Exec=java -jar /path/to/myapp.jar
Icon=java
Terminal=false

Place it in ~/.local/share/applications/.

Running JAR Files With Arguments

Many JAR applications accept command-line arguments. Pass them after the file name:

java -jar myapp.jar --config /path/to/config.properties

You can also pass system properties using the -D flag:

java -Dmyapp.debug=true -jar myapp.jar

Check the application’s help flag for available options:

java -jar myapp.jar --help

Using A GUI File Manager

If you prefer not to use the terminal, most file managers support running JAR files. Navigate to the file, right-click, and select “Open With Java Runtime.”

If this option is missing, install the Java plugin for your file manager. For Nautilus (GNOME), install nautilus-actions and configure it.

For Thunar (XFCE), use thunar-archive-plugin.

Automating JAR File Execution

You can create a shell script to run the JAR file with specific settings. Create a file named run.sh:

#!/bin/bash
java -jar /opt/myapp/myapp.jar --port 8080

Make it executable:

chmod +x run.sh

Then run it with ./run.sh.

For system startup, add the command to your crontab:

@reboot java -jar /opt/myapp/myapp.jar &

Or create a systemd service file for better control.

Running JAR Files From A Different Directory

You don’t have to be in the same directory. Provide the full path:

java -jar /opt/apps/myapp.jar

If the JAR file needs resources from its own directory, use the -Duser.dir flag:

java -Duser.dir=/opt/apps -jar /opt/apps/myapp.jar

Debugging JAR File Issues

If the application crashes, increase the Java heap size:

java -Xmx1024m -jar myapp.jar

This allocates 1024 MB of memory. Adjust as needed.

Enable verbose output:

java -verbose:class -jar myapp.jar

This shows which classes are being loaded, helping identify missing dependencies.

Check the Java console for error messages. Redirect output to a file:

java -jar myapp.jar > output.log 2>&1

Security Considerations

Only run JAR files from trusted sources. JAR files can contain malicious code. Verify the digital signature if available:

jarsigner -verify myapp.jar

If you see “jar verified,” the signature is intact. If not, be cautious.

Run untrusted JAR files in a sandboxed environment, like a Docker container or a virtual machine.

Alternative Methods To Run JAR Files

Some JAR files are designed to be run with a launcher script. Check if the package includes a run.sh or start.sh file.

You can also use the java command with the -jar option from a Python script using subprocess:

import subprocess
subprocess.run(['java', '-jar', 'myapp.jar'])

For headless servers, consider using xvfb-run if the JAR file requires a graphical interface:

xvfb-run java -jar myapp.jar

Updating Java On Linux

Keep Java updated for security and compatibility. On Debian/Ubuntu:

sudo apt update
sudo apt upgrade

For specific Java versions, use:

sudo apt install openjdk-17-jre

Check for available versions:

apt search openjdk

Uninstalling Java

If you no longer need Java, remove it:

sudo apt remove default-jre

For other distributions, use the corresponding package manager.

Frequently Asked Questions

How do I run a JAR file in Linux terminal?

Use the command java -jar filename.jar. Ensure Java is installed and you are in the correct directory.

Why is my JAR file not running in Linux?

Common reasons include missing Java, incorrect file permissions, a corrupted JAR file, or a missing main class. Check the error message for clues.

Can I run a JAR file without Java?

No. JAR files require the Java Runtime Environment. You must install JRE or JDK to execute them.

How do I make a JAR file executable in Linux?

Set execute permissions with chmod +x filename.jar, then run it with java -jar filename.jar. You can also associate it with Java in your file manager.

What is the difference between JRE and JDK for running JAR files?

JRE is sufficient for running JAR files. JDK includes development tools like compilers and debuggers, which are not needed for execution.

Final Tips For Running JAR Files Smoothly

Always check the application’s documentation for specific requirements. Some JAR files need additional libraries or environment variables.

Use the -version flag to verify your Java installation matches the application’s needs. If you encounter memory issues, adjust the heap size.

Consider using a Java version manager like SDKMAN to switch between versions easily:

curl -s "https://get.sdkman.io" | bash
sdk install java 17.0.2-open

This is especially useful if you work with multiple Java applications.

If you are a developer, you might want to compile your own JAR files. Use the jar command from the JDK:

jar cf myapp.jar -C build/classes .

This creates a JAR from compiled class files.

Remember that the terminal is your friend. Once you get comfortable with the commands, running JAR files becomes second nature. The process is consistent across most Linux distributions, with only minor differences in package names.

If you still face issues, check the system logs with journalctl -xe or look for Java-specific logs in /var/log/. Community forums like Stack Overflow and the Java subreddit are also excellent resources.

Now you have all the knowledge to handle any JAR file on Linux. Start with a simple test file to confirm your setup works, then move on to your actual applications. The command java -jar will become one of your most used tools.