Wednesday, May 26, 2010

The Java Archive Tool (JAR) Examples


Project structure

Here's the project structure, your will create a JAR file name "test.jar"

/workspace/test/classes/com/mkyong/awt/AwtExample.class
/workspace/test/classes/com/mkyong/awt/AwtExample2.class
/workspace/test/classes/manifest.txt

Assume you are in "/workspace/test/classes/"

1. Create jar file

Create a Jar file which include the 'AwtExample.class"

jar -cvf test.jar com/mkyong/awt/AwtExample.class

Create a Jar file which include the all classes

jar -cvf test.jar com/mkyong/awt/*.class

2. Update jar file

Update test.jar by adding a new class "AwtExample2.class"

jar -uvf test.jar com/mkyong/awt/AwtExample2.class

3. Extract jar file

Extract all files from test.jar

jar -xvf test.jar

Extract only "AwtExample.class" from test.jar

jar -xvf test.jar com/mkyong/awt/AwtExample.class

Extract all files from "com" folder in test.jar

jar -xvf test.jar com

4. List table of contents of jar file

List all contents of "test.jar"

jar -tf test.jar

5. Add manifest into jar file

 jar -cvfm example.jar manifest.txt com/mkyong/awt/*.class

6. Make jar file executable

Add "Main-Class" and entry point in your manifest file

Main-Class: com.mkyong.awt.AwtExample

Jar them all

jar -cvfm AwtExample.jar manifest.txt com/mkyong/awt/*.class


No comments:

Post a Comment