Java Tools Tutorials - Herong's Tutorial Examples - v6.23, by Herong Yang
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 .\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":
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 .\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:
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 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:
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 \ com.herongyang.util.HelloModularized Hello world! - Modularized
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