Create and Extract Jar File on Linux, MacOS and Windows

Java ARchive (JAR) files are the standard and portable way to pack up all the parts of your Java application into a compact bundle for distribution or installation. You can put whatever you want into a JAR file: Java class files, serialized objects, data files, images, audio, etc. The Java runtime system can load .class files directly from a Jar file present in the CLASSPATH. The non-class files (data, images, etc.) can also be retrieved from the classpath using the getResource() method. 

Items stored in JAR files are compressed with the standard ZIP file compression (generally shrinks by about 40%) which makes downloading classes over a network much faster.

The jar utility provided with the JDK is a simple tool for creating and extracting the JAR files. It mimics the Unix tape archive command, tar.

1. Cheat Sheet for Linux, MacOS and Windows

Let’s start with a cheat sheet for creating and extracting JAR files on Linux, macOS, and Windows:

Task (Windows, Linux and MacOS)Command
Create a JAR File that contains all of the files in the directory app/.jar -cvf app.jar file1 file2 …
jar –create –verbose –file app.jar /app
Create a JAR File that contains all of the files listed in a text file classes.list.jar –create –verbose –file app.jar @classes.list
Add a Manifest Filejar –create –file app.jar –manifest mymanifest.mf /app
Create a Runnable Jar Filejar –create –file –main-class com.howtodoinjava.app.HelloWorld app.jar /app
Extract a JAR File,  optionally extracting just path(s).
This command always extracts in the current directory.
jar -xvf app.jar
jar –extract –verbose –file app.jar
List the contents of a Jar File, optionally showing just path(s).jar -tvf app.jar
jar –list –verbose –file app.jar

Now let us go through the process of creating and extracting JAR files on different operating systems: Linux, macOS, and Windows.

2. Creating a JAR File

To create a JAR file, you can use the jar command included in the Java Development Kit (JDK).

The basic syntax for creating a JAR file is the same across all three operating systems:

jar -cvf jarFile path [ path ] [ …​ ]
  • jar is the command.
  • cf stands for “create file”.
  • v stands for verbose. In verbose mode, you get information about file sizes, modification times, and compression ratios.
  • jarfile.jar is the name of the JAR file you want to extract.

For example, to create a JAR file called app.jar containing the classes in ‘app/classes‘ directory, images in ‘app/images‘ directory and configuration in ‘app/config‘ directory, we run the following command to pack everything in the jar:

jar -cvf app.jar app/

This command packages the specified files into a JAR file, ready for distribution or execution. Because we requested verbose output, the jar command will output what it is packaging as well.

3. Making a Runnable JAR file

The jar command automatically adds a directory called /META-INF which holds files describing the contents of the JAR file. It always contains at least one text file: MANIFEST.MF. The manifest is, by default, mostly empty and contains only JAR file version information:

Manifest-Version: 1.0
Created-By: 1.7.0_07 (Oracle Corporation)

Aside from default and other attributes, you can put a few special values in the manifest file such as Main-Class. Its value is the class containing the primary main() method for the application contained in the JAR.

Main-Class: com.howtodoinjava.app.Application

Now we can supply the manifest information to jar command using the flag ‘m‘.

jar -cvmf manifest.mf app.jar app/

Alternatively, we can pass the –main-class attribute in the command itself:

jar -cvf app.jar --main-class com.howtodoinjava.app.Application app/

Now you can run the application directly from the JAR. It will start the application from the com.howtodoinjava.app.Application.main() method.

% java -jar app.jar

4. Extracting a JAR File

Unpacking a JAR file is just like unzipping a ZIP file. To extract the contents of a JAR file, you can use the jar command as well. The files are placed in the correct hierarchy.

The syntax for extracting JAR files is the same on Linux, macOS, and Windows:

jar -xvf jarfile.jar
  • jar is the command.
  • xf stands for “extract file”.
  • v stands for verbose.
  • jarfile.jar is the name of the JAR file you want to extract.

For example, to extract the contents of a JAR file named mylibrary.jar, you would run:

jar -xvf app.jar

This command will extract all the files contained within the JAR and place them in the current directory.

We can also extract an individual file or directory by supplying one more command-line argument:

jar -xvf app.jar app/config/myconfig.properties

5. Conclusion

Creating and extracting JAR files is a straightforward process, and the commands are consistent across different operating systems. Simply follow the provided commands, and you’ll be able to create and extract JAR files with ease on Linux, macOS, and Windows.

Happy Learning !!

Comments

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.