What a JAR file is and why you'd make one

A JAR file (Java Archive) is a single container that holds compiled Java code, resources like images or text files, and metadata about what's inside. Think of it like a ZIP file — because it actually is one — but formatted so Java can run the code directly from it. You create a JAR file when you want to package your Java program so other people can run it without needing the original source code or having to compile anything themselves.

The main reason to make a JAR file is distribution. Instead of handing someone a folder full of .class files and hoping they put them in the right place, you give them one .jar file. They can double-click it to run your program (if it has a main entry point), or other developers can import it into their own projects as a library.

Key Takeaways

  • A JAR file is a compressed archive that bundles compiled Java code and resources into one portable package.
  • You create a JAR using the jar command-line tool that comes with Java, not by manually zipping files.
  • You need a manifest file to tell Java which class contains the main method if you want the JAR to run directly.
  • The basic command is jar cf jarname.jar *.class, but the exact syntax depends on your folder structure and whether you want a runnable JAR.

Compile your Java code first

Before you can make a JAR file, your .java source files must be compiled into .class files. Open a terminal or command prompt, navigate to the folder containing your .java files, and run the Java compiler.

If you have a single file called HelloWorld.java, type:

javac HelloWorld.java

If you have multiple files in the same folder, compile them all at once:

javac *.java

After compilation, you should see .class files in the same folder as your source code. These are what goes into the JAR. If compilation fails, fix the errors in your source code and try again — the JAR tool cannot fix Java syntax problems.

Create a manifest file if you want a runnable JAR

A manifest file is a plain text file that tells Java which class to run when someone opens your JAR. It is optional if your JAR is just a library that other programs will use, but required if you want people to double-click the JAR and have it execute.

Create a file called manifest.txt (or any name you choose) in the same folder as your compiled .class files. Open it in any text editor and type exactly this:

Main-Class: ClassName

Replace ClassName with the name of the class that contains your public static void main(String[] args) method. For example, if your main class is called HelloWorld, write:

Main-Class: HelloWorld

Save the file and make sure there is a blank line at the end. This blank line is required by the JAR specification, and without it Java may not read the manifest correctly.

Run the jar command to create the archive

The jar command is part of the Java Development Kit (JDK) and works from the command line. Navigate to the folder containing your .class files and manifest (if you made one), then run the appropriate command below.

For a JAR with a manifest (runnable):

jar cfm myprogram.jar manifest.txt *.class

The letters mean: c = create, f = file (the next argument is the filename), m = manifest (the next argument is the manifest file). The order matters — manifest comes before the files to include.

For a JAR without a manifest (library only):

jar cf myprogram.jar *.class

Replace myprogram.jar with whatever you want to name your JAR file. If you have resource files like images or configuration files in the same folder, include them too:

jar cfm myprogram.jar manifest.txt *.class *.png *.txt

After you run the command, you should see the .jar file appear in the same folder. You can verify it worked by listing the folder contents.

Handle packages and nested folder structures

If your Java code uses packages (folders that organize classes), the folder structure matters. The jar command preserves the directory layout, which is what you want — Java needs to find classes in the right package path when the JAR is imported.

For example, if your code is organized like this:

src/com/mycompany/HelloWorld.classsrc/com/mycompany/Utils.class

Navigate to the src folder and run:

jar cfm myprogram.jar manifest.txt com/

This includes the entire com folder and preserves the package structure inside the JAR. When someone imports your JAR, they can use import com.mycompany.HelloWorld; and Java will find the class.

If your manifest specifies a main class, use the fully may have access to name (with the package):

Main-Class: com.mycompany.HelloWorld

Test your JAR file

If you created a runnable JAR with a manifest, test it by double-clicking it or running it from the command line:

java -jar myprogram.jar

If it runs without errors, your JAR is working. If you see an error like "no main manifest attribute", the manifest file was not included correctly — go back and check that you used the m flag in the jar command and that your manifest.txt file has the correct class name and a blank line at the end.

If you created a library JAR, test it by creating a new Java project, adding your JAR to the classpath, and trying to import and use one of your classes. The exact steps depend on your IDE, but the principle is the same: point the IDE to your .jar file and it should recognize the classes inside.

Frequently Asked Questions

Can I edit a JAR file after I create it?

Technically yes — a JAR is just a ZIP file, so you can open it with any archive tool and add or remove files. However, this is not recommended for distribution. If you need to change something, modify your source code, recompile, and create a new JAR. This ensures the code is consistent and the manifest is correct.

What if I don't have a main class or don't want a runnable JAR?

Skip the manifest entirely and use jar cf myprogram.jar *.class. This creates a library JAR that other Java programs can import and use. You do not need a main method for this to work.

How do I include external libraries my code depends on?

The JAR tool does not automatically bundle dependencies. You have two options: include the .class files from those libraries in your JAR (if you have permission), or document that users need to add those libraries to their classpath separately. Most projects use a build tool like Maven or Gradle to handle this automatically.

Why does my JAR file not run when I double-click it?

The most common reasons are: no manifest file, wrong class name in the manifest, or the manifest missing a blank line at the end. Also, the user's computer must have Java installed and the .jar file type associated with Java. Test from the command line first with java -jar myprogram.jar to see the actual error message.

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

Yes. Both IDEs have built-in export or package options that create JAR files for you without typing command-line commands. Look for "Export" or "Build Artifact" in the menu. The result is the same as using the jar command, but the IDE handles the details.