Creating Module JAR File

This section provides a tutorial example on how to use 'jar --create' command to create a module JAR file to store all class files of a Java module.

If your Java program is packaged in a Java module (which was introduced in Java 9 in 2017), you can store all classes of the module into a JAR file to make a module JAR file. This can be done by using the normal "jar --create" or "jar --update" command.

Here is the source file directory structure of a simple Java module called "com.herongyang":

C:\herong>tree /F .\src
.\SRC
|---com.herongyang
    |   module-info.java
    |
    |---com
        |---herongyang
            |---util
                    HelloModularized.java

The module-info.java source code is shown below:

/* module-info.java
 * Copyright (c) 2018 HerongYang.com. All Rights Reserved.
 */
module com.herongyang {
    requires java.base;
    exports com.herongyang.util;
}

The HelloModularized.java source code is shown below:

/* HelloModularized.java
 * Copyright (c) 2018 HerongYang.com. All Rights Reserved.
 */
package com.herongyang.util;
public class HelloModularized {
   public static void main(String[] a) {
      System.out.println("Hello world! - Modularized"); 	
   }
}

I used the following command to compile my module "com.herongyang":

C:\herong>javac --module com.herongyang 
   -d .\cls --module-source-path .\src 

Here is the module class file directory structure generated by the above "javac" command:

C:\herong>tree /F .\cls
.\CLS
|---com.herongyang
    |   module-info.class
    |
    |---com
        |---herongyang
            |---util
                    HelloModularized.class

Then I created a module JAR file by archiving the entire class file directory tree:

C:\herong>jar --create --verbose --file com.herongyang.jar 
   -C .\cls\com.herongyang .

added manifest
added module-info: module-info.class
adding: com/(in = 0) (out= 0)(stored 0%)
adding: com/herongyang/(in = 0) (out= 0)(stored 0%)
adding: com/herongyang/util/(in = 0) (out= 0)(stored 0%)
adding: com/herongyang/util/HelloModularized.class(in = 472) (out= 315)
 

Now com.herongyang.jar is a module JAR file, because it a module-info.class in the JAR's root directory.

I can use the "jar --describe-module" to verify the module information:

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

Or I can use the "java --module" command to run the HelloModularized class from the module:

C:\herong>java --module-path com.herongyang.jar 
   --module com.herongyang/com.herongyang.util.HelloModularized

Hello world! - Modularized

The class file directory structure in the module JAR file, com.herongyang.jar, is also backward compatible. In other words, I can use it as a non-module JAR file and access it's class directly:

C:\herong>java -classpath com.herongyang.jar 
   com.herongyang.util.HelloModularized
   
Hello world! - Modularized

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