JAR File Format and 'jar' Tool
Part:
1
2
3
4
5
(Continued from previous part...)
hello.jar - My First JAR File
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:
>javac Hello.java
>jar cvf hello.jar Hello.class
added manifest
adding: Hello.class(in = 416) (out= 285)(deflated 31%)
>jar tf hello.jar
META-INF/
META-INF/MANIFEST.MF
Hello.class
>del Hello.class
>jar xvf hello.jar
created: META-INF/
extracted: META-INF/MANIFEST.MF
extracted: Hello.class
>dir Hello.class
416 Hello.class
>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.
JAR Files Are ZIP Files
The JAR specification says that "JAR file is a file format based on the popular ZIP file format".
So are JAR files really ZIP files? Let's do some tests to find out.
Test 1. Create a JAR file, test.jar, with "jar" command. Rename test.jar to test.zip. Unzip test.zip with WinZIP.
You should have no problem to extract all files out of test.zip with WinZIP.
Test 2. Create a ZIP file, tutu.zip, with WinZIP. Rename tutu.zip to tutu.jar. Extract files from tutu.jar with
"jar". You should have no problem to extract all files out of tutu.jar with "jar" command.
Test 3. Create a password protected ZIP file, secure.zip, with WinZIP. Rename secure.zip to secure.jar.
Extract files from secure.jar with "jar". You should get a run time exception like this:
>jar tf secure.jar
java.io.FileNotFoundException: secure.jar (The system cannot find the
file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:106)
at java.io.FileInputStream.<init>(FileInputStream.java:66)
at sun.tools.jar.Main.run(Main.java:185)
at sun.tools.jar.Main.main(Main.java:904)
So JAR files and ZIP files are fully compatible, as long as no encryptions are used on ZIP files.
I guess I don't need WinZIP anymore.
(Continued on next part...)
Part:
1
2
3
4
5
|