How to Archive a Package with the jar Command in Java - dummies

How to Archive a Package with the jar Command in Java

By Doug Lowe

The most common use for the jar utility in Java is to create an archive of an entire package. The procedure for doing that varies slightly depending on what operating system you’re using. However, the jar command itself is the same regardless of your operating system. Here’s the procedure for archiving a package on a PC running any version of Windows:

  1. Open a command window.

    The easiest way to do that is to choose Start→Run, type cmd in the Open text box, and click OK. On Windows 8, right-click the Start button, and then choose Command Prompt on the menu that appears.

    If you have trouble running the jar command in Step 3, you may need to open the command prompt in Administrator mode. To do so, click the Start menu, type cmd, right-click cmd.exe at the top of the Start menu, and choose Run as Administrator.

  2. Use a cd command to navigate to your package root.

    For example, if your packages are stored in c:javaclasses, use this command:

    cd javaclasses
  3. Use a jar command that specifies the options cf, the name of the jar file, and the path to the class files you want to archive.

    For example, to create an archive named utils.jar that contains all the class files in the com.lowewriter.util package, use this command:

    jar cf utils.jar comlowewriterutil*.class
  4. To verify that the jar file was created correctly, use the jar command that specifies the options tf and the name of the jar file.

    For example, if the jar file is named utils.jar, use this command:

    jar tf utils.jar

    This lists the contents of the jar file so you can see what classes were added. Here’s some typical output from this command:


    As you can see, the utils.jar file contains the Console class, which is in the com.lowewriter.util package.

  5. That’s all!

    You’re done. You can leave the jar file where it is, or you can give it to your friends so they can use the classes it contains.