Java Tools Tutorials - Herong's Tutorial Examples - v6.23, by Herong Yang
"jar --module-version" - Updating Module Version in JAR
This section provides a tutorial example on how to use 'jar --update --module-version' to update the version number of the Java module stored in a module JAR file.
When Java module was introduced in Java 9 in 2017, it does not support module versioning.
But you can add a version number to the module stored in a module JAR file using the "jar --update --module-version" command. You can update it using the same command.
Right after I created my module JAR file, com.herongyang.jar, the module have no version number:
herong> jar --describe-module --file com.herongyang.jar com.herongyang jar:file:///C:/herong/com.herongyang.jar/!module-info.class exports com.herongyang.util requires java.base main-class com.herongyang.util.HelloModularized
Then I updated the module JAR with "jar --update --version-number" command:
herong> jar --update --verbose --file com.herongyang.jar \ --module-version 1.00 --main-class com.herongyang.util.HelloModularized updated manifest updated module-info: module-info.class
Notice that:
To verify the version number update, run the "jar --describe-module" command again:
herong> jar --describe-module --file com.herongyang.jar com.herongyang@1.00 jar:file:///C:/herong/com.herongyang.jar/!module-info.class exports com.herongyang.util requires java.base main-class com.herongyang.util.HelloModularized
Cool. The version number "1.00" is appended to the module name in the module-info file.
Or maybe not so cool. Why should I allow "jar" command to update my class file, module-info.class, without updating my source file, module-info.java? This will make my source file out of sync with the class file in the JAR.
Also, when I decompile the updated module-info.class file, I got an invalid source code!
herong> jar --extract --verbose --file com.herongyang.jar \ module-info.class inflated: module-info.class herong> javap module-info.class > module-info.java herong> type module-info.java Compiled from "module-info.java" module com.herongyang@1.00 { requires java.base; exports com.herongyang.util; }
Remove the message in the first line in module-info.java with a text editor, and try to compile it:
herong> type module-info.java module com.herongyang@1.00 { requires java.base; exports com.herongyang.util; } herong> javac module-info.java module-info.java:1: error: '{' expected module com.herongyang@1.00 { ^ 1 error
Conclusion: "jar --update --module-version" command hacked the "module-info.class" in the module JAR file to support module versioning. Don't use this feature for now.
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