Java Tool Tutorials - Herong's Tutorial Notes
Dr. Herong Yang, Version 5.00

Creating the First JAR File - hello.jar

This section provides a tutorial example on how to create a JAR file with the 'jar' command.

To create my first JAR file, I wrote the following Java file, Hello.java:

class Hello {
   public static void main(String[] a) {
      System.out.println("Hello world!"); 	
   }
}

Here is what I did in a command window to create and extract my first JAR file, hello.jar:

C:\herong>javac Hello.java

C:\herong>jar cvf hello.jar Hello.class
added manifest
adding: Hello.class(in = 416) (out= 285)(deflated 31%)

C:\herong>jar tf hello.jar
META-INF/
META-INF/MANIFEST.MF
Hello.class

>del Hello.class

C:\herong>jar xvf hello.jar
  created: META-INF/
extracted: META-INF/MANIFEST.MF
extracted: Hello.class

C:\herong>dir Hello.class
   416 Hello.class

C:\herong>type meta-inf\manifest.mf
Manifest-Version: 1.0
Created-By: 1.4.2 (Sun Microsystems Inc.)


What happened here is that:

  • My first "jar" command created a new JAR file called hello.jar. "jar" automatically added "manifest".
  • My second "jar" command displayed what is in hello.jar. As you can see, the first "jar" command actually added a directory called META-INF into the jar file.
  • My third "jar" command extracted all files out of hello.jar.
  • My "type" command showed you what was added as "manifest": two package attributes: version and create-by.

Sections in This Chapter

JAR - Java Archive File Format

'jar' - JAR File Tool Command and Options

Creating the First JAR File - hello.jar

Managing JAR Files with WinZIP

META-INF/MANIFEST.MF - JAR Manifest File

Adding META-INF/MANIFEST.MF to JAR Files

Using JAR Files in Java Class Paths

Creating Executable JAR Files

Dr. Herong Yang, updated in 2008
Creating the First JAR File - hello.jar