Create JAR File

The JAR file format is a compressed format used primarily to distribute Java applications and libraries. It is built on the ZIP file format, and functions in a similar way; many files are compressed and packaged together in a single file, making it easy to distribute the files over a network. If you need to package a Java application or library, you can create a JAR file using the Java Development Kit (JDK) and your computer's command prompt.

Steps

Windows

  1. Prepare your files. Place all the files you want to include in the JAR file inside a single folder. They will need to be referenced through a single command line, so specifying separate paths is not feasible.
  2. Open the command prompt. This can be done by clicking on Start and then Run. Type "cmd" into the text box and click the "OK" button.
  3. Navigate to the folder where you stored your files. By default, the command prompt will read "C:\>."
    • To navigate deeper into the hard drive, use the "change directory" command by typing "cd."
    • For example, if your files are stored at "C:\myfiles," then you should type "cd \myfiles."
    • To go directly to the directory, hold down shift and right click on the folder in explorer. Then select "Open command window here".
  4. Set the path to the directory of the JDK bin. You will need to run the jar.exe utility to create a JAR file, and that file is located in the bin directory.
    • Use the "path" command to specify the JDK bin directory. For example, if you installed JDK to the default location, you would type: "path c:\Program Files\Java\jdk1.5.0_09\bin"
    • If you aren't sure of the exact directory, navigate to it in Windows Explorer and take note of the directory's full path.
  5. Create the JAR file. The format of the command line for creating the JAR file looks like this: "jar cf 'jar-file'.jar input-file(s)."
    • The "jar" portion refers to the jar.exe program, which compiles the JAR file.
    • The "c" option specifies that you want to create a JAR file
    • The "f" option means that you want to specify the filename.
    • The "jar-file" portion is where you should type the name that you want the file to have.
    • "Input-file(s)" is a space-separated list of all the files to be included in the JAR file.
    • For example, you might type "jar cf myjar manifest.txt myclass.class." This would create a JAR file with the filename "myjar.jar" which would include the files "manifest.txt" and "myclass.class."
    • If you add directories to the JAR file, the jar.exe utility will automatically add their contents.

Mac

  1. Prepare your files. Put all the files you want to include in the JAR file inside a single folder.
    • Open a Terminal command prompt, and set it to the target directory where your JAR files are located.
  2. Compile the .java class. For example, compile HelloWorld.java with:
    • javac HelloWorld.java
    • This will produce a .class file needed for the JAR file.
  3. Create a manifest file. Save it using the extension .txt using the text editor and input the following:
    • Main-Class: HelloWorld (or whatever your file's name is )
    • Note: this manifest file must end with a newline character
  4. Create the JAR file using this code:
    • jar cfm Manifest.txt HelloWorld.jar HelloWorld.class
  5. Run the file:java -cp filename.jar mainclass

Tips

  • You can digitally sign your JAR files to improve their security. This can be done using the "jarsigner" command in the JDK.
  • You can also create JAR files with standard compression utilities, like the kind used for creating ZIP files. When doing this, be careful to specify that the manifest file is the first one included in the archive.

Things You'll Need

  • Computer
  • Java Development Kit

Sources and Citations