Java Tools Tutorials - Herong's Tutorial Examples - v6.24, by Herong Yang
"java --module" - Launching Program from Module
This section provides a tutorial example on how to use 'java --module' command to launch a program that is packaged in a Java module.
If your Java program is packaged in a Java module (which was introduced in Java 9 in 2017), you can launch your program using the "java --module" command.
Launch a program requires you to specify the program main class name with module name as a prefix.
For example, I have a program class called HelloModularized.java packaged in a Java module called com.herongyang. Here is the source file directory structure of the module:
herong> tree /F .\src .\SRC |---com.herongyang | module-info.java | |---com |---herongyang |---util HelloModularized.java
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
Here is how I used the "java --module" command to launch my program:
herong> java --module-path .\cls \ --module com.herongyang/com.herongyang.util.HelloModularized Hello world! - Modularized
Cool. "java --module" command works! Note that:
"--module-path" option must be specified before "--module" option. Otherwise, you will get the "Module ... not found" exception:
herong> java --module com.herongyang/com.herongyang.util.HelloModularized \ --module-path .\cls Error occurred during initialization of boot layer java.lang.module.FindException: Module com.herongyang not found
You can still launch a program in a module as a class. You just need to specify the class path to the module sub-directory:
herong> java -classpath .\cls\com.herongyang \ com.herongyang.util.HelloModularized Hello world! - Modularized
Table of Contents
javac - The Java Program Compiler
►java - The Java Program Launcher
java - Program Launching Command and Options
Launching Hello.java - My First Java Program
"java -classpath" - Specifying Class Path
"java -jar" - Specifying Executable JAR File
"java -X" - Specifying Non-Standard Options
"java --list-modules" - Listing Modules in JDK
"java --describe-module" - Printing Module Definition
►"java --module" - Launching Program from Module
"java --module" - Launching Program from Module JAR
javaw - Launching Java Programs without Console
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