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

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

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

Here is how I used the "java --module" command to launch my program:

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

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

C:\herong>java -classpath .\cls\com.herongyang 
   com.herongyang.util.HelloModularized

Hello world! - Modularized



 

Table of Contents

 About This Book

 Java Tools Terminology

 Java Tools Included in JDK

 javac - The Java Program Compiler

 jar - The JAR File Tool

 jlink - The JRE Linker

 jmod - The JMOD File Tool

 jimage - The JIMAGE File Tool

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

 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

 native2ascii - Native-to-ASCII Encoding Converter

 Outdated Tutorials

 References

 Full Version in PDF/EPUB