Linux

How to Run Jar Files in Linux

Run Jar Files in Linux

Java Archive (JAR) files allow Java software to be distributed as a single executable package containing all components and resources required to run the application. Being able to execute JAR files is essential for Linux users looking to run Java-based software on their systems. This comprehensive guide will walk you through everything you need to know about running JAR files on Linux, covering topics like:

  • Prerequisites for running JAR files
  • Installing the Java Runtime Environment (JRE)
  • Using the command line to launch JAR files
  • Running JAR files through graphical user interfaces
  • Troubleshooting issues with launching JAR files

Follow along with the clear, step-by-step instructions provided, and you’ll be able to successfully get Java applications packaged as JAR files up and running on your Linux machine.

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

If you prefer clicking on JAR files rather than using the terminal, Linux desktop environments provide graphical ways to execute them as well. Here are some options:

File Manager Context Menu

Most Linux file managers like Nautilus (GNOME Files), Dolphin, Thunar, etc. automatically register the .jar file type to allow executing it from the right-click context menu.

Simply right-click the JAR file, hover over “Open With”, and select “Java” or any installed JRE from the menu. This will launch the JAR application.

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.

Custom File Associations

You can also manually associate .jar files to open with a JRE when double-clicked.

On GNOME, use the “Other Application” option in Preferences -> File Associations to set .jar files to always launch with a java -jar command.

So in summary, all major Linux GUIs include ways to execute JAR files graphically without needing to use the terminal.

r00t

r00t is a seasoned Linux system administrator with a wealth of experience in the field. Known for his contributions to idroot.us, r00t has authored numerous tutorials and guides, helping users navigate the complexities of Linux systems. His expertise spans across various Linux distributions, including Ubuntu, CentOS, and Debian. r00t's work is characterized by his ability to simplify complex concepts, making Linux more accessible to users of all skill levels. His dedication to the Linux community and his commitment to sharing knowledge makes him a respected figure in the field.
Back to top button