Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Create and Execute JAR Files in Linux

· 5 min read · Home Tech

In this article
  1. What Is a JAR File?
  2. Prerequisites: Installing the JDK
  3. Step-by-Step: Creating a Simple Executable JAR
  4. Including Multiple Classes and Resources
  5. Running a JAR: The java -jar Command
  6. Common Errors and How to Fix Them
  7. Making a JAR Directly Executable (Optional)
  8. Frequently Asked Questions

To create and execute a JAR file in Linux, you use the jar command (part of the JDK) to package compiled Java classes and resources, and the java -jar command to run it. This article walks through the complete process, from writing a simple Java program to running the final executable JAR, including how to set the main class and handle common errors.

What Is a JAR File?

A JAR (Java Archive) file is a package format that bundles multiple Java class files, metadata, and resources (like images or configuration files) into a single compressed file, based on the ZIP format. It is the standard way to distribute Java applications and libraries. An executable JAR contains a manifest file (META-INF/MANIFEST.MF) that specifies the entry point—the class with the main method—so the JVM knows what to run when you execute it.

Prerequisites: Installing the JDK

Before you can create or run a JAR, you need a Java Development Kit (JDK) installed. The jar tool is included in the JDK, not the JRE. On most Linux distributions, you can install it with:

  • Debian/Ubuntu: sudo apt install default-jdk
  • Fedora/RHEL: sudo dnf install java-17-openjdk-devel
  • Arch Linux: sudo pacman -S jdk-openjdk

Verify the installation with java -version and javac -version. If you only have the JRE, you’ll see a “command not found” for jar.

Step-by-Step: Creating a Simple Executable JAR

Let’s create a minimal Java program and package it as an executable JAR.

  1. Write the Java source file. Create a file named Hello.java with the following content:
    public class Hello {
        public static void main(String[] args) {
            System.out.println("Hello from a JAR!");
        }
    }
  2. Compile it. Run javac Hello.java. This produces Hello.class.
  3. Create a manifest file. Create a text file named MANIFEST.MF (or use any name) with the line:
    Main-Class: Hello
    Make sure there is a newline after the last line, otherwise the JAR tool may ignore it.
  4. Package the JAR. Use the jar command:
    jar cfm hello.jar MANIFEST.MF Hello.class
    • c – create a new archive
    • f – specify the output file name
    • m – include the manifest file
    Alternatively, you can use the --main-class option to avoid a separate manifest file:
    jar cfe hello.jar Hello Hello.class
    Here, e sets the entry point.
  5. Run the JAR. Execute it with:
    java -jar hello.jar
    You should see the output: Hello from a JAR!

Including Multiple Classes and Resources

Real applications have many classes and resources. You can add them all to the JAR by listing them or using wildcards. For example, if you have packages com/example/ and a resources/ folder, you can do:

jar cfe app.jar com.example.Main com/example/*.class resources/

You can also use the @ argument to read file names from a text file, which is handy when you have many files. The jar command preserves directory structure, so your package names remain intact.

Running a JAR: The java -jar Command

The java -jar command tells the JVM to run the JAR as an executable. It requires the manifest to contain a Main-Class attribute. If you omit the -jar flag and try java Hello.jar, you’ll get an error because the JVM expects a class name, not a JAR file.

You can pass arguments to your program after the JAR name:

java -jar app.jar arg1 arg2

Inside your main method, these are available in the String[] args array.

If you need to set JVM options (like memory limits), place them before the -jar flag:

java -Xmx512m -jar app.jar

Common Errors and How to Fix Them

  • “no main manifest attribute” – The JAR was created without a Main-Class entry. Recreate it with jar cfe or add the manifest correctly.
  • “Error: Could not find or load main class” – The class name in the manifest is wrong, or the class file is not in the correct package path inside the JAR. Check the manifest and the JAR contents with jar tf.
  • “java.lang.UnsupportedClassVersionError” – The JAR was compiled with a newer Java version than the runtime. Either update your JRE/JDK or recompile with a lower --release flag.
  • “Permission denied” when running – This happens if you try to execute the JAR directly (e.g., ./app.jar). You must use java -jar, or make the JAR executable with chmod +x and use a shebang line (see below).

Making a JAR Directly Executable (Optional)

On Linux, you can make a JAR file runnable like a script by adding a shebang line at the top of the manifest. For example, create a manifest file with:

#!/bin/sh
exec java -jar "$0" "$@"
exit 1

Then create the JAR with jar cfm app.jar manifest.txt ... and make it executable with chmod +x app.jar. Now you can run ./app.jar directly. Note that the JAR format still expects the manifest to start with Manifest-Version, but the JVM ignores the shebang line when using java -jar.

Frequently Asked Questions

How do I view the contents of a JAR file?

Use jar tf yourfile.jar to list all entries. This is helpful to verify that your classes and manifest are in the right place.

Can I run a JAR without the JDK?

Yes, you only need a JRE (Java Runtime Environment) to run a JAR. However, to create a JAR you need the JDK because the jar tool is part of it.

What is the difference between jar and zip?

A JAR is a ZIP file with a specific structure and a manifest. You can unzip a JAR with unzip, but the manifest is essential for Java to treat it as an application or library.

How do I include external libraries in my JAR?

You can either bundle the library classes inside your JAR (fat JAR) or use the Class-Path attribute in the manifest to point to external JARs. For example: Class-Path: lib/other.jar.

Why does my JAR run on one machine but not another?

Most likely a Java version mismatch or missing dependencies. Check that the target machine has a compatible JRE and that all required libraries are present.

Can I create a JAR from an IDE like Eclipse or IntelliJ?

Yes, most IDEs have an “Export as JAR” option that handles the manifest and packaging automatically. But the command-line method gives you more control and is useful for scripting.