Jar files (Java ARchive files) can contain Java class files that will run when the jar is executed. A jar is an archiving format that not only stores directories and source files, but can be run as an executable as well.

Requirements

A Java development environment, such as access to a filesystem, text editor, java compiler (javac), Java runtime environment (JRE/java), and jar utility.

Procedure

First, you will need to create a directory structure that will match what you wish to contain in the jar. This directory structure will contain your .class files. Next, in each source file (.java), you will need to assign them to their directory structure location. This can be done with the package statement. In a .java source file, you can add it as:

package application.packageName.subPackageName;
public class ClassOne{
}

This will add ClassOne to the application.packageName.subPackageName package. You must place the compiled class in this location (in the application\packageName\subPackageName directory on Windows or application/packageName/subPackageName directory on Linux).

Once you have the source .java files, you must compile them into .class files with a Java compiler (javac).

When using directories that contain your source files, it is important to note that Linux uses the / character to separate directories/folders and Windows uses \.

Next, you will need to know which class contains your execution entry point, or main method:

public static void main(String[] args){}

This can be in any .class file. When you create an executable jar file, not only will it create an internal directory structure with .class files, it will also create:

META-INF/
META-INF/MANIFEST.MF

Which is a directory that creates the MANIFEST.MF file. This is created automatically. You need to specify within this MANIFEST.MF file what your execution entry point class is. There are two ways of doing this. First, you can create a .txt file that contains edits to the MANIFEST.MF file. You can create a blank .txt file named MANIFEST.txt containing:

Main-Class: application.packageName.subPackageName.ClassOne

The MANIFEST.MF file is formatted as header/value pairs. The line you are editing must also end with a newline (\n).

To create an executable jar file, you can use the following syntax:

jar -cfm JarOne.jar MANIFEST.TXT application

This syntax can be used from the command line if you are in the directory that contains the application directory structure. The -cfm argument will let you create a jar with -c, specify the name of the jar with -f and specify edits to the MANIFEST.MF with -m. The order in which the next arguments follow is specific. The jar file’s name is next, followed by the MANIFEST.MF edits .txt file, followed by the directory structure to include, with their .class files.

The second way to specify the execution entry point to the .jar file is to edit the MANIFEST.MF from the command line:

jar -cfe JarOne.jar application.packageName.subPackageName.ClassOne application

where application.packageName.subPackageName.ClassOne is the execution entry point and the -e argument in -cfe stands for the application entry point.

Then, you will have a .jar file created in your current working directory. You can run the executable jar file with:

java -jar JarOne.jar