Jar vs. Zip Files: The Key Differences

Last Updated on November 25, 2022

If you want to build or execute jar files, it’s essential to understand the differences and similarities compared to zip files. So in this article you’ll discover everything about jar vs. zip files, with practical tips and examples along the way.

How do you identify a jar vs. zip file?

Identifying a jar or zip files is easy. Jar files end in a .jar extension and zip files end in .zip.

This naming convention helps you know how to handle each type of file, which we’ll get into next.

What is a zip file?

A zip file is a single file which contains within it several other files. This is known as an archive. A zip archive is a way of packaging multiple files into a single file for convenient handling and storage.

As well as being an archive, a zip file is also compressed. This means that if you add up the size of all the files contained in the zip file, the number you come to would be larger than the file itself.

Compression example

For example, imagine we have 2 files we want to add to a new zip archive:

  • diary.txt (size 5kb) containing all my deepest secrets
  • recipes.txt (size 10kb) containing my family Lasagna recipe

Thanks to compression, the resulting zip file size might be only 8kb instead of 15kb (5kb + 10kb).

How well the files are compressed depends on what’s in them. For example, video .mp4 files are already heavily compressed so putting them in a zip file won’t make them much smaller.

Zip file summary

  • ends in .zip extension
  • contains multiple files (i.e. it’s an archive)
  • contains compressed files (saving storage space)

What is a jar file?

A jar file is a specific type of archive used by the Java programming language. It’s a single file which contains multiple files, most importantly compiled Java .class files.

Since a jar file contains code, it can be used in 2 main ways by developers:

  1. when running a Java application, a jar file can be added to the classpath to make available any code contained within
  2. the jar file itself can be run as an executable application

You’ll see examples of these later, but just know for now that a jar file is a convenient container of Java code. It’s also compressed in the same way as zip files, as described above.

Since jar files let you store code in a single file, it’s easy to publish Java libraries in jar format to be used by other developers. Many Java libraries are available in the Maven Central Repository, such as the useful apache-commons-lang3 library.

jar file META-INF directory

As well as compiled Java bytecode, a jar file contains metadata files in its META-INF directory.

The most important of these is MANIFEST.MF where you can specify a list of key/value pairs. You can add any key/values you like, but some have a special meaning to Java.

For example:

  • Manifest-Version describes the specific manifest file format being used
  • Main-Class specifies the class which should be executed when running the jar file
Manifest-Version: 1.0
Main-Class: com.tomgregory.AmazingApplication

To learn about other manifest file attributes and files contained within META-INF, consult Oracle’s jar file specification.

Jar file summary

  • ends in .jar extension
  • contains multiple Java class files (i.e. it’s an archive)
  • contents compressed in the same was as zip
  • can be added to Java classpath
  • can be executed from the command line
  • contains META-INF/MANIFEST.MF with metadata

What are the similarities between jar and zip files?

As mentioned, jar files and zip files are both archives and are both compressed.

In fact, jar files use the same archive and compression techniques as zip files. So jar files are actually a specific type of zip file.

JAR file is a file format based on the popular ZIP file format and is used for aggregating many files into one. A JAR file is essentially a zip file that contains an optional META-INF directory. 

from JAR File Specification

This all means you can open a jar file using the same tools you use to open a zip file.

What are the differences between jar and zip files?

Here’s a summary of the main differences, some of which have already been mentioned:

  • jar files end .jar but zip files end .zip
  • jar files contain mainly Java code but zip files contain anything
  • jar files can contain META-INF/MANIFEST.MF in a specific format, but zip files normally don’t *
  • jar files can be added to an application’s classpath, but zip files normally cannot *
  • jar files can be executed, but zip files normally cannot *

* jar files are a subset of zip files, so if a zip file followed the jar specification it could be used as a jar file

How do you create a zip file?

In Windows right click on any selection of files or directories and select Compress to Zip file.

Compress to zip file

This creates a zip file which you can use as you like. Send it to your friends or upload it to the cloud for safe keeping!

Resulting zip file

When you’re ready to access your files again you can either:

  • double click the zip file to access the contents
  • right click the zip file and select Extract All…

How do you create a jar file?

The best way to create a jar file is on the terminal using the jar command, included in the Java Development Kit (JDK). Make sure you have a JDK installed and ensure you’ve added the Java bin directory to your PATH environment variable.

Let’s assume we have a class located in build/classes/java/main, which is where the popular build automation tool Gradle generates classes.

We can create a jar file for this Java code with a single command.

$ jar --create --file my-amazing-application.jar -C build/classes/java/main/ .
  • --create tells the jar tool that we want to create a jar file
  • --file specifies the name of the created jar file
  • -C sets the directory in which to find classes
  • the final argument is a list of classes, in our case . which includes everything

After running the command we have a shiny new jar file, ready to run (maybe).

$ ls my-amazing-application.jar

Looking inside the jar file

To see what the jar command did, let’s look inside the jar file by extracting it with this command:

jar --extract --file my-amazing-application.jar

jar as zip: remember that a jar file is a specific type of zip file, so you can also extract it in the same way as zip files using the techniques described earlier.

This reveals:

  • a directory containing classes, in my case a single class com/tomgregory/AmazingApplication.class
  • a manifest file META-INF/MANIFEST.MF

Cool, so the jar command automatically created a manifest file. What’s inside?

Manifest-Version: 1.0
Created-By: 17.0.2 (Oracle Corporation)

The command by default sets these key/values to get you started. There’s Manifest-Version (described earlier) and Created-By which is set to the Java version used to generate the jar file.

Let’s see if this will be enough to execute the jar file.

Executing a jar file

You can run a jar file using the java command, also bundled in the JDK, like this.

$ java -jar my-amazing-application.jar
no main manifest attribute, in my-amazing-application.jar

Ooops! This happens because there’s no Main-Class attribute in MANIFEST.MF, so Java doesn’t know which class to execute. Yeah, even though there’s only one class to choose from!

Anyway, we can fix this issue by building the jar again with the --main-class option.

$ jar --main-class com.tomgregory.AmazingApplication --create --file my-amazing-application.jar -C build/classes/java/main/ .

This generates a new MANIFEST.MF, now including the Main-Class attribute:

Manifest-Version: 1.0
Created-By: 17.0.2 (Oracle Corporation)
Main-Class: com.tomgregory.AmazingApplication

And we should be able to execute the jar file:

$ java -jar my-amazing-application.jar
Welcome to the wonderful world of jar!

Awesome! The Java application executed successfully.

Adding jar to classpath

Another way to use a jar file is to add it to the classpath when executing a Java application. This means the code of the Java application can access and use the code within the jar file.

How does that work?

When compiling or running Java code, pass the -classpath option and include the jar file on it.

For example, if we had an application HelloWorld which relied on my-amazing-application.jar we built earlier, we run the application by adding the jar to the classpath like this:

$ java -classpath my-amazing-application.jar HelloWorld.java

When we do that any code within the jar is available at runtime for our application to use.

Final thoughts

You now know the most important differences between jar and zip files, helping you create and use these files effectively whatever the situation.

In this article we’ve touched on some commands included in the Java Development Kit (JDK), like jar and java. To continue learning the JDK essentials and how it compares to the JRE, I recommend reading JDK vs. JRE: The Key Differences.

Jar vs. Zip Files: The Key Differences

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top