Linux

How to Run Jar Files in Linux

Run Jar Files in Linux

Java Archive (JAR) files are a crucial part of Java applications, as they package all the necessary components, such as classes, images, and metadata, into a single, compressed file. Running JAR files on Linux systems is a straightforward process, but it requires some basic knowledge of the command line and Java runtime environment. In this comprehensive guide, we will walk you through the steps to run JAR files on Linux, including prerequisites, command-line execution, making JAR files executable, running them with a double-click (GUI), and automating their execution. By the end of this article, you will have a solid understanding of how to run JAR files on your Linux system effectively.

Prerequisites for Running JAR Files

In order to execute JAR files on Linux, the Java Runtime Environment (JRE) needs to be installed. The JRE provides a minimal set of requirements for running Java applications, including the Java Virtual Machine (JVM), core class libraries, and other components.

Most mainstream Linux distributions like Ubuntu, Debian, Fedora, openSUSE, etc. have package repositories that contain JRE packages, making the installation process straightforward. Specific instructions for installing the JRE on popular distros are covered later in this guide.

Once you have the JRE set up on your system, you can then use either the command line terminal or graphical user interfaces to launch JAR files. The method you use depends on your comfort level with the Linux shell and personal preferences.

Installing Java Runtime Environment on Linux

Here are step-by-step instructions for installing JRE on some of the most widely-used Linux distributions:

Installing JRE on Ubuntu and Debian

To set up JRE on Ubuntu or related Debian-based distros like Linux Mint, follow this process using the apt package manager:

sudo apt install default-jre

Verify that the JRE is installed properly by checking its version:

java -version

This outputs details about the installed JRE and confirms it is correctly set up.

Installing JRE on openSUSE

For openSUSE Leap/Tumbleweed installations, here is how JRE can be set up:

sudo zypper install java-11-openjdk

Confirm a suitable JRE is now active:

java -version

Installing JRE on Other Linux Distributions

For other distros, search their package repositories for available JRE/JDK packages. Some options are:

  • On MX Linux, use:
sudo apt install default-jre
  • On Kali Linux, use:
sudo apt install default-jre
  • On CentOS, use:
sudo yum install java-11-openjdk
  • On AlmaLinux, use:
sudo dnf install java-latest-openjdk
sudo yum install java-11-openjdk

This covers the installation process for most common Linux distributions. Now we can move on to actually running JAR files.

Running JAR Files via Command Line

Once JRE is installed, JAR files can be executed from the terminal using the java command. Here is the basic syntax:

java -jar <name-of-jar-file>

For example, to launch a file called app.jar, use:

java -jar app.jar

The file needs to have executable permissions for this to work. To avoid permissions errors, set permissions before running the JAR:

chmod +x app.jar
java -jar app.jar

Some additional options you can pass to java -jar:

  • To set the maximum Java heap size (2GB in this example):
java -jar -Xmx2G app.jar
  • To see JAR verification warnings:
java -jar -Xverify:none app.jar
  • To disable Java cache when launching JAR:
java -jar -Xint app.jar
  • To run the JAR file silently in the background instead of spawning a new window:
java -jar app.jar &

This covers the key things you need to know to launch JAR files from the command line terminal on Linux.

Running JAR Files via Graphical User Interfaces

Running JAR files with a double-click in a graphical user interface (GUI) requires configuring file associations. File associations tell your Linux desktop environment which application to use when opening a specific file type. To associate JAR files with the Java runtime in popular Linux desktop environments, follow these steps:

GNOME (Ubuntu, Fedora)

  1. Right-click on a JAR file and select “Properties”.
  2. In the “Open With” tab, click on “Add”.
  3. Select “OpenJDK Java Runtime” or “Oracle Java Runtime” from the list of applications.
  4. Click “Add” and then “Set as default”.
  5. Close the Properties window.

KDE (Kubuntu, KDE Neon)

  1. Right-click on a JAR file and select “Properties”.
  2. In the “General” tab, click on the “File Type Options” button.
  3. In the “Application Preference Order” section, click on “Add”.
  4. Select “OpenJDK Java Runtime” or “Oracle Java Runtime” from the list of applications.
  5. Click “OK” and then “Apply”.
  6. Close the Properties window.

After configuring file associations, you can double-click on a JAR file to run it using the Java runtime.

Desktop Launchers

Another approach is using menu launchers that let you create shortcuts to run JAR files, similar to Windows .exe or Linux .desktop files. For example in KDE Plasma, you can:

  1. Right-click the JAR file and create a launcher.
  2. Check “Run in terminal” option if you want terminal output.
  3. Launch the JAR by double-clicking the launcher icon.

GNOME, Xfce, and other desktops have similar functionality to generate launchers.

Conclusion

Running JAR files on Linux systems is a straightforward process that can be accomplished through various methods, including the command line, GUI, and automated execution. By following the steps and best practices outlined in this guide, you can effectively run JAR files on your Linux system and troubleshoot common issues that may arise. Remember to ensure JAR file portability, optimize execution with JVM arguments and leverage the power of IDEs for development and testing. With this knowledge, you can confidently deploy and run Java applications on Linux systems.

r00t

r00t is an experienced Linux enthusiast and technical writer with a passion for open-source software. With years of hands-on experience in various Linux distributions, r00t has developed a deep understanding of the Linux ecosystem and its powerful tools. He holds certifications in SCE and has contributed to several open-source projects. r00t is dedicated to sharing her knowledge and expertise through well-researched and informative articles, helping others navigate the world of Linux with confidence.
Back to top button