"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:

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

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

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

C:\herong>jar --extract --verbose --file com.herongyang.jar 
  module-info.class

 inflated: module-info.class

C:\herong>javap module-info.class > module-info.java

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

C:\herong>type module-info.java
module com.herongyang@1.00 {
  requires java.base;
  exports com.herongyang.util;
}

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

Last update: 2018.

Table of Contents

 About This Book

 Java Tools Terminology

 Java Tools Included in JDK

 javac - The Java Program Compiler

jar - The JAR File Tool

 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

 Creating Module JAR File

"jar --module-version" - Updating Module Version in JAR

 jlink - The JRE Linker

 jmod - The JMOD File Tool

 jimage - The JIMAGE File Tool

 java - The Java Program Launcher

 jdb - The Java Debugger

 jcmd - The JVM Diagnostic Tool

 jconsole - Java Monitoring and Management Console

 jstat - JVM Statistics Monitoring Tool

 JVM Troubleshooting Tools

 jvisualvm (Java VisualVM) - JVM Visual Tool

 jmc - Java Mission Control

 javap - The Java Class File Disassembler

 keytool - Public Key Certificate Tool

 jarsigner - JAR File Signer

 jshell - Java Language Shell

 native2ascii - Native-to-ASCII Encoding Converter

 Outdated Tutorials

 References

 Full Version in PDF/EPUB