JAR File Format and 'jar' Tool
Part:
1
2
3
4
5
(Continued from previous part...)
Creating Executable JAR Files
JAR files can be used to distribute supporting classes. They can also be used to distribute main classes
for stand-alone applications.
If you put a stand-alone application into a JAR file, you could make it executable by adding the
"Main-Class" attribute to the manifest file.
Here is what I did to add my F2C class to herong.jar and make it executable.
First I created a manifest file, fs_f2c.txt:
Main-Class: F2C
Then I added F2C class to herong.jar:
>javac -classpath herong.jar F2C.java
>jar uvmf fs_f2c.txt herong.jar F2C.class
updated manifest
adding: F2C.class(in = 919) (out= 551)(deflated 40%)
>jar tf herong.jar
META-INF/
META-INF/MANIFEST.MF
herong/
herong/TempratureConvertorBean.class
F2C.class
Now herong.jar contains a small stand-alone application. But how to execute this JAR file?
That's easy. You can run a JAR file with the "java -jar" command like this:
>java -jar herong.jar 60.0
Fahrenheit = 60.0
Celsius = 15.555555555555555
My TempraturConvertorBean - Version 1.00
Cool. Right? But you can make it even better. You can register JAR files to be automatically
opened by the "java -jar" command on Windows system to make JAR files "truly" executable.
Here is what I did to make this happen.
Warning: this following tutorial modifies Windows registry database. Please do not follow it
if you don't have enough knowledge about Windows system.
1. Go to Start > Run. And enter "regedit" followed by clicking OK. Registry Editor window shows up.
2. Go to [HKEY_CLASSES_ROOT\jarfile\shell\open\command]. You should see the current value like this:
("C:\Program Files\Java\j2re1.4.2\bin\javaw.exe" -jar "%1" %*). This was added when you install JDK
on your system.
3. Double click this value and change it to: ("C:\Program Files\Java\j2re1.4.2\bin\java.exe" -jar "%1" %*).
4. Open a command window and try this:
>herong.jar 50
Fahrenheit = 50.0
Celsius = 10.0
My TempraturConvertorBean - Version 1.00
Better. Right? You can run any JAR files by just typing their file names now.
You can also double click any JAR files on Window Explorer now. Try it and see what happens.
Conclusions
- JAR files are ZIP files.
- JAR files can have attributes stored in the META-INF/MANIFEST.MF file.
- JAR files can be used in Java class paths.
- JAR files can be "executable".
Part:
1
2
3
4
5
|