A JAR file is a compressed container that holds Java code, resources, and metadata in a single package

A JAR file (Java Archive) is essentially a ZIP file with a specific structure that Java can recognize and run. It bundles your compiled Java classes, images, configuration files, and other resources into one file. Instead of distributing dozens of individual .class files, you create one JAR that contains everything a program needs to run.

JAR files serve two main purposes: they let you package a complete Java process for distribution, and they let other developers use your code as a library in their own projects. When you read a Java program or library, it often arrives as a .jar file.

You create a JAR file using the jar command, which comes built into Java. The process takes your compiled code (the .class files), organizes them with a manifest file that describes what's inside, and compresses everything together.

Key Takeaways

  • You need compiled Java files (.class files) before you can create a JAR — the jar command does not compile source code for you.
  • A manifest file tells Java which class contains the main method and other information about your JAR, and you can let the jar command create a basic one automatically.
  • The jar command syntax is jar cf jarname.jar classfiles, where c means create and f means file.
  • You can verify what's inside a JAR by running jar tf jarname.jar to list the contents without extracting them.
  • If your JAR is meant to run as a standalone program, the manifest must specify the main class, or users will get an error when they try to run it.

Compile your Java source files first

Before you can create a JAR, you need compiled Java bytecode. Open your terminal or command prompt and navigate to the directory where your .java files live. Run the Java compiler on each file or on all of them at once.

If your files are in a folder called src and you want the compiled .class files to go into a folder called bin, you would run:

javac -d bin src/*.java

The -d bin part tells the compiler where to put the output. After this runs, you should see .class files in your bin folder. If you see error messages instead, fix the Java syntax errors in your source code and try again.

Create a manifest file if your JAR needs a main entry point

A manifest file is a plain text file named MANIFEST.MF that tells Java metadata about your JAR. If you want your JAR to run as a standalone process (not just as a library), the manifest must specify which class contains the main method.

Create a file called manifest.txt in your project directory. Open it in any text editor and add this line:

Main-Class: com.example.MyMainClass

Replace com.example.MyMainClass with the actual package and class name that contains your main method. If your class is not in a package, just write the class name. Add a blank line at the end of the file — Java's manifest parser requires it.

If you are creating a JAR that other code will use as a library (not run directly), you do not need a manifest file at all. The jar command can create a basic one for you automatically.

Run the jar command to package your files

Navigate to the directory where your compiled .class files are located (or use the full path). The basic syntax is:

jar cf myapp.jar -C bin .

This breaks down as: jar (the command), c (create a new JAR), f (the next argument is the filename), myapp.jar (what to name it), -C bin (change to the bin directory), and . (include everything in it).

If you created a manifest file and want to include it, use:

jar cfm myapp.jar manifest.txt -C bin .

The m flag tells jar to use your manifest file. The order matters: the manifest filename comes right after the JAR filename.

After the command runs, you should see a new .jar file in your current directory. If you see an error, check that the bin directory exists and contains .class files.

Verify the contents of your JAR

Before you distribute or run your JAR, check that everything is inside it. Use the list command:

jar tf myapp.jar

This prints out every file in the JAR without extracting anything. You should see your .class files organized in folders that match their package names. If your manifest is in there, you will see META-INF/MANIFEST.MF at the top of the list.

If a file is missing or in the wrong place, delete the JAR and run the jar command again with the correct path or directory.

Run your JAR if it has a main class

If your manifest specifies a Main-Class, you can run the JAR directly:

java -jar myapp.jar

Java reads the manifest, finds the main class you specified, and runs it. If you get an error saying "no main manifest attribute", the manifest file is missing or does not have the Main-Class line. Go back and add it, then recreate the JAR.

If your JAR is a library that other code imports, other developers do not run it directly — they add it to their project's classpath and call the classes inside it.

Organize your JAR files for long-term storage

Once you have created a JAR, store it in a dedicated folder with a clear naming scheme. Include the version number in the filename: myapp-1.0.jar, myapp-1.1.jar, and so on. This way, if you need to go back to an older version, you know exactly which file to use.

Keep your source code (.java files) and your compiled JAR files in separate directories. Many developers use a structure like src/ for source, bin/ for compiled classes, and dist/ or release/ for finished JAR files. This separation makes it straightforward to rebuild the JAR if you change the source code.

If you are distributing your JAR to others, include a README file that explains what the JAR does, which Java version it requires, and how to run it. This saves users from guessing.

Frequently Asked Questions

Can I add files other than .class files to a JAR?

Yes. You can include images, configuration files, text files, and other resources. Place them in the same directory structure as your classes before you run the jar command, and they will be packaged together. Other code can load these resources from inside the JAR using the ClassLoader.

What if I need to update a JAR after I create it?

You can extract files from a JAR, modify them, and add them back using the jar command with the u (update) flag. However, it is usually cleaner to modify your source code, recompile, and create a new JAR with a new version number. This keeps your history clear.

Do I need to use a build tool like Maven or Gradle?

For small projects, the jar command is enough. For larger projects with many dependencies, build tools automate the process and handle libraries for you. They are not required, but they save time once your project grows.

Why does my JAR file not run even though I specified a main class?

The most common reason is that the Main-Class line in the manifest has the wrong class name or package. Double-check the spelling and make sure the class actually contains a public static void main method. Also verify that the .class file for that class is actually inside the JAR by running jar tf.

Can I create a JAR from the command line on Windows?

Yes. The jar command works the same way on Windows, Mac, and Linux. Use backslashes for paths on Windows if you are not using the -C flag, or use forward slashes with the -C flag, which works on all systems.