Java Tools Tutorials - Herong's Tutorial Examples - v6.24, by Herong Yang
"jar --main-class" - Making JAR File Executable
This section provides a tutorial example on how to use 'jar --main-class ...' command option to specify the application main class to make a JAR file executable.
You can also make a JAR file "executable", if you:
Here is what I did to add my JAR file, herong.jar, executable using the "jar --update --main-class ..." command:
herong> jar --update --verbose --file herong.jar --main-class F2C updated manifest
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:
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\jdk-17.0.1\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\jdk-17.0.1\bin\java.exe" -jar "%1" %*).
4. Open a command window and try this:
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.
Table of Contents
javac - The Java Program Compiler
java - The Java Program Launcher
JAR - Java Archive File Format
jar - JAR File Tool Command and Options
"jar --create" - Creating New JAR File
"jar --list" - Listing Files in JAR File
"jar --extract" - Extracting Files from JAR File
Managing JAR Files with WinZIP
META-INF/MANIFEST.MF - JAR Manifest File
Adding META-INF/MANIFEST.MF to JAR Files
"jar -C" - Changing Input Directory
Using JAR Files in Java Class Paths
"jar --update" - Updating Class Files in JAR
►"jar --main-class" - Making JAR File Executable
"jar --module-version" - Updating Module Version in JAR
jpackage - Binary Package Builder
javadoc - The Java Document Generator
jdeps - The Java Class Dependency Analyzer
jdeprscan - The Java Deprecated API Scanner
jcmd - The JVM Diagnostic Tool
jconsole - Java Monitoring and Management Console
jstat - JVM Statistics Monitoring Tool
jhsdb - The Java HotSpot Debugger
jvisualvm (Java VisualVM) - JVM Visual Tool
javap - The Java Class File Disassembler
keytool - Public Key Certificate Tool
jrunscript - Script Code Shell
native2ascii - Native-to-ASCII Encoding Converter