“JAR files are packaged with the zip file format, so you can use them for tasks such as lossless data compression, archiving, decompression, and archive unpacking. These tasks are among the most common uses of JAR files, and you can realize many JAR file benefits using only these basic features.
“To perform basic tasks with JAR files, you use the Java Archive Tool provided as part of the Java Development Kit (JDK). The Java Archive tool is invoked by using the
jar command.” (Abridged from Lesson: Packaging Programs in JAR Files; see Resources below.)
Create a stand-alone application
To create a stand-alone application (a JAR), ensure that at least one of your classes contains a main method with the standard signature:
public static void main(String argument)
The instructions which follow assume:
- the JAR file is to be named
- all of the files required for the application are in a single directory
- all of the classes are in the root of the single directory
- the class containing the main method is named
- non-Java files are in subdirectories, of the single directory, with appropriate names like images and sounds
Of course, you should modify the instructions to reflect the circumstances of your particular application.
Create the JAR
At the command line of your operating system, navigate to the single directory which contains your Java application and type the following command:
jar cvfe HelloWorld.jar HelloWorld *.class images sounds
Here’s the breakdown of the command:
jarmeans run the Java Archive Tool
- c: create the JAR
- v: use verbose mode (show each step as creation proceeds)
- f: put the resulting JAR in the specified file (
- e: set the entry point—the class which contains the main method—to the specified class (
*.classmeans include all of the bytecode files found in the single directory
imagesmeans include all of the files found in the
soundsmeans include all of the files found in the
To execute the JAR file from the command prompt, type:
java -jar HelloWorld.jar
to execute the JAR file from your operating system’s GUI, double-click it.
The Java Tutorials: Lesson: Packaging Programs in JAR Files
[This page last updated 2020-12-23 at 12h13 Toronto local time.]
© 2007–2021 Hersch, Bear & Company Limited. All rights reserved. “Grammar Authority,” “grammarauthority.com,” “touque.ca,” and “Canada’s Thinking Cap” are trademarks of Hersch, Bear & Company Limited. All other trademarks and copyrights are the property of their respective owners.