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

herong> tree /F .\src

The source code is shown below:

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

The source code is shown below:

 * Copyright (c) 2018 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":

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:

herong> tree /F .\cls
    |   module-info.class

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

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:

herong> jar --describe-module --file com.herongyang.jar

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:

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:

herong> java -classpath com.herongyang.jar \

Hello world! - Modularized

Table of Contents

 About This Book

 Java Tools Terminology

 Java Tools Included in JDK

 javac - The Java Program Compiler

 java - The Java Program Launcher

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



 "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

 jpackage - Binary Package Builder

 javadoc - The Java Document Generator

 jdeps - The Java Class Dependency Analyzer

 jdeprscan - The Java Deprecated API Scanner

 jdb - The Java Debugger

 jcmd - The JVM Diagnostic Tool

 jconsole - Java Monitoring and Management Console

 jstat - JVM Statistics Monitoring Tool

 JVM Troubleshooting Tools

 jhsdb - The Java HotSpot Debugger

 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

 jrunscript - Script Code Shell

 Miscellaneous Tools

 native2ascii - Native-to-ASCII Encoding Converter

 JAB (Java Access Bridge) for Windows

 Archived Tutorials


 Full Version in PDF/EPUB