Jar Files

A Jar file is essentially a collection of files (mostly archives of java classes) in one file, similar to a ZIP file. A Jar file facilitates the packaging of applications into one file. In addition to the Java class files and resources, a Jar file can contain a META-INF directory. The most important file in this directory is manifest.mf. It is used to define extension and package related data. The Main-Class: classname in the manifest file specifies the class that gets executed first.

Running A Jar File

To run a Jar file at the command line, enter the following command:

   java -jar jar-file 

When using a GUI, you can also simply double click the Jar file icon.

Compiling A Jar File

To compile a Java file and include a Jar file, enter the following command line:

   javac -classpath jar-file java-file

For multiple JAR files, separate the jar-files with semicolons, as follows:

   javac -classpath jar-file1;jar-file2;jar-file3 java-file

Some rules to note:

To include a Jar file when running a compiled java file, enter the following command:

   java -classpath jar-file class-file

Creating Jar Files

To create a Jar file, enter the following command:

   jar cf jar-file input-files

For example, if you want to make a C.jar with A.class and B.class, enter the following command:

   jar cf C.jar A.class B.class

Java's Jar Help