Creating Executable JAR Files

This section provides a tutorial example on how to create 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:

C:\herong>javac -classpath herong.jar F2C.java

C:\herong>jar uvmf fs_f2c.txt herong.jar F2C.class
updated manifest
adding: F2C.class(in = 919) (out= 551)(deflated 40%)

C:\herong>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:

C:\herong>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:

C:\herong>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.

Last update: 2015.

Table of Contents

 About This Book

 Java Tools Terminology

 Installing Java 8 on Windows

 'javac' - The Java Program Compiler

 'java' - The Java Program Launcher

 'jdb' - The Java Debugger

 'jconsole' - Java Monitoring and Management Console

 'jstat' - JVM Statistics Monitoring Tool

 JVM Troubleshooting Tools

 jvisualvm (Java VisualVM) - JVM Visual Tool

'jar' - The JAR File Tool

 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

 'javap' - The Java Class File Disassembler

 'keytool' - Public Key Certificate Tool

 'native2ascii' - Native-to-ASCII Encoding Converter

 Outdated Tutorials

 References

 PDF Printing Version