Home Ubuntu A beginner’s guide to running JAR files in Ubuntu

A beginner’s guide to running JAR files in Ubuntu

We'll show you how to run JAR files in Ubuntu, walking you through the installation of necessary tools and the execution process. Enhance your Ubuntu experience by unlocking the potential of Java applications and running JAR files with ease.

by Abraham
running jar files in ubuntu

If you are a developer or an avid user of Java applications, you may have come across jar files, which are Java archive files that contain Java classes and other resources. Running jar files in Ubuntu Linux can be a bit different from running them in other operating systems, as Linux has its own set of commands and conventions.

In this tutorial, we will discuss various methods to run jar files in Ubuntu Linux, along with installation examples on other Linux distributions. Whether you are a Java developer or a Linux enthusiast, this guide will provide you with step-by-step instructions and insights to help you execute jar files on your Ubuntu Linux system with ease.

Running a JAR files in Ubuntu

Method 1: Using the Java Virtual Machine (JVM)

The most common and straightforward method to run a jar file in Ubuntu Linux is using the Java Virtual Machine (JVM), an essential component of any Java installation. Here’s how you can do it:

Step 1: Install Java Development Kit (JDK)

Before you can run a jar file in Ubuntu, you must install Java on your system. If you don’t have it already, you can install the Java Development Kit (JDK) by following these steps:

Open a terminal window.

Update your package list by running the following command:

sudo apt update
update system resources

Update system resources

Install the default JDK package by running the following command:

sudo apt install default-jdk
install default jdk package

Install default jdk package

Enter your password when prompted, and press Y to confirm the installation.

Step 2: Navigate to the directory

Once you have Java installed on your system, navigate to the directory where the jar file is located using the ‘cd’ command. For example, if your jar file is located in the ‘Documents’ folder, you can navigate to it by running the following command:

cd Documents
navigate to documents where jar file is located

Navigate to documents where jar file is located

Note: Replace ‘Documents’ with the actual directory name where your jar file is located.

Step 3: Run the jar file

After navigating to the directory containing the jar file, you can run it using the ‘java’ command followed by the ‘-jar’ option and the name of the jar file. Here’s an example:

java -jar myfile.jar

Replace ‘myfile.jar’ with the actual name of your jar file.

Example:

java -jar logisim-generic-2.7.1.jar
launch logisim.jar file

Launch logisim.jar file

Method 2: Using the ‘java’ command with classpath

Another way to run a jar file in Ubuntu Linux is by using the ‘java’ command with the ‘-cp’ or ‘-classpath’ option, which allows you to specify the classpath for the jar file. The classpath is a set of directories or jar files where Java looks for classes and resources. Here’s how you can do it:

Step 1: Navigate to the directory

Like Method 1, navigate to the directory where the jar file is located using the ‘cd’ command in the terminal.

navigate to documents where jar file is located

Navigate to documents where jar file is located

Step 2: Run the jar file with Classpath

To run the jar file with classpath, use the ‘java’ command followed by the ‘-cp’ or ‘-classpath’ option, and specify the path to the jar file. Here’s an example:

java -cp myfile.jar:my.package.java

Replace ‘myfile.jar’ with the actual name of your jar file and ‘my.package.MyClass’ with the actual package and class name of the main class in the jar file.

Example:

To execute the iLoveFossLinux file using the logisim.jar file, we shall execute the following line of code:

javac -cp logisim-generic-2.7.1.jar: iLoveFossLinux.java


java -cp logisim-generic-2.7.1.jar: iLoveFossLinux
run jar file with classpath

Run jar file with classpath

Method 3: Making the jar file executable

In Ubuntu, you can also make a jar file executable, similar to how you can make a shell script or a binary executable. This can be useful if you want to run the jar file as a standalone application without using the ‘java’ command. Here’s how you can do it:

Step 1: Create a manifest file

To make the jar file executable, you need to create a manifest file that specifies the main class to run when the jar file is executed. Here’s how you can create a manifest file:

Create a new file named ‘manifest.txt’ in the same directory as the jar file.

touch manifest.txt
create new text file

Create new text file

Open the file using a text editor.

sudo nano manifest.txt
open file using text editor

Open file using text editor

Add the following lines to the file, replacing ‘my.package.MyClass’ with the actual package and class name of the main class in the jar file.

Manifest-Version: 1.0

Main-Class: my.package.MyClass
append the following lines

Append the following lines

Save and close the file.

Step 2: Update the jar file

After creating the manifest file, you need to update the jar file to include the manifest file. Here’s how you can do it:

Update the jar file using the ‘jar’ command with the ‘-uf’ option, followed by the jar file’s name and the manifest file’s name.

jar -uf myfile.jar manifest.txt

Replace ‘myfile.jar’ with the actual name of your jar file.

Example:

jar -uf logisim-generic-2.7.1.jar manifest.txt
update jar file

Update jar file

Step 3: Make the jar file executable

Finally, you can make the jar file executable using the ‘chmod’ command, which changes the file mode bits to allow the file to be executed. Here’s an example:

chmod +x myfile.jar

Replace ‘myfile.jar’ with the actual name of your jar file.

Example:

chmod +x logisim-generic-2.7.1.jar
make jar file executable

Make jar file executable

Now you can execute the jar file by double-clicking on it or by running it from the terminal using the following command:

./myfile.jar

Replace ‘myfile.jar’ with the actual name of your jar file.

Example:

./logisim-generic-2.7.1.jar
launch logisim.jar file

Launch logisim.jar file

Running a jar file as a service

If you want to run a jar file as a service on Ubuntu Linux, you can create a systemd service unit file. A systemd service unit file is a configuration file that defines how a service should behave, including the command to start the service and any dependencies it has.

Here are the steps to create a systemd service unit file for a jar file:

Open a terminal and create a new service unit file in the /etc/systemd/system directory using a text editor like Nano or Vim.

sudo nano /etc/systemd/system/my-service.service
create a new service unit file

Create a new service unit file

Paste the following contents into the file, making sure to replace the placeholders with the appropriate values:

[Unit]

Description=My Service

After=syslog.target

[Service]

WorkingDirectory=/path/to/jar/file

ExecStart=/usr/bin/java -jar my-service.jar

SuccessExitStatus=143

[Install]

WantedBy=multi-user.target
    1. The ‘Description‘ field is a brief description of the service.
    2. The ‘After‘ field specifies that the service should start after reaching the syslog target.
    3. The ‘WorkingDirectory‘ field specifies the directory where the jar file is located.
    4. The ‘ExecStart‘ field specifies the command to start the service, which in this case is running the jar file using Java.
    5. The ‘SuccessExitStatus‘ field specifies the exit status code indicating the service has stopped successfully.
    6. The ‘WantedBy‘ field specifies the target that the service should be started on.
      append the following lines to the service unit file

      Append the following lines to the service unit file

Save and close the file.

Reload the systemd configuration to load the new service unit file.

sudo systemctl daemon-reload
reload system configuration

Reload system configuration

Enable the service to start automatically at boot time.

sudo systemctl enable my-service.service
enable the service to start at boot time

Enable the service to start at boot time

Start the service.

sudo systemctl start my-service.service
start service

Start service

You can check the status of the service by running the following command:

sudo systemctl status my-service.service
check service status

Check service status

If the service is running correctly, you should see a status message indicating that it is active.

Troubleshooting

If you encounter any issues while running the jar file, here are some common troubleshooting tips:

  1. Check the Java version: Ensure you have the correct version of Java installed by running the ‘java -version‘ command in the terminal. If you don’t have the right version installed, you may need to install it or update your system.
  2. Check the manifest file: Ensure you have created a valid manifest file with the correct main class specified. You can check the contents of the manifest file by running the ‘jar tf‘ command, followed by the name of the jar file.
  3. Check the file permissions: Make sure that the jar file has the correct permissions to execute. You can employ the ‘ls -l‘ command to check the file permissions.
  4. Check for dependencies: Ensure all the required dependencies are installed and available in the classpath. You can check the classpath by running the ‘echo $CLASSPATH‘ command in the terminal.

Conclusion

Running jar files on Ubuntu Linux is a straightforward process that can be done using a few simple steps. With the installation of Java, the creation of a manifest file, and the running of the jar file, users can access the functionality of their desired program. The process can be further enhanced by creating a systemd service unit file that will run the jar file as a service, ensuring the program is always available and ready for use.

It is essential to note that the process of running jar files may vary slightly on different Linux distributions. Still, the basic principles are the same, and with a bit of research, users can adapt the process to their specific distribution. This flexibility and adaptability are what make Linux a favorite among developers and power users.

You may also like

Leave a Comment

fl_logo_v3_footer

ENHANCE YOUR LINUX EXPERIENCE.



FOSS Linux is a leading resource for Linux enthusiasts and professionals alike. With a focus on providing the best Linux tutorials, open-source apps, news, and reviews written by team of expert authors. FOSS Linux is the go-to source for all things Linux.

Whether you’re a beginner or an experienced user, FOSS Linux has something for everyone.

Follow Us

Subscribe

©2016-2023 FOSS LINUX

A PART OF VIBRANT LEAF MEDIA COMPANY.

ALL RIGHTS RESERVED.

“Linux” is the registered trademark by Linus Torvalds in the U.S. and other countries.