"javac --module" - Compiling Entire Module

This section provides a tutorial example on how to use 'javac --module' command to compile all Java source code in an entire module.

If your Java program is packaged in a Java module (which was introduced in Java 9 in 2017), you can compile all Java source files in the module using the "javac --module" command.

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

herong> tree /F .\src
    |   module-info.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");

In order compile all Java source files in a given module, we actually need 3 options:

In my case, 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 output file directory structure:

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

I can run the HelloModularized.java program from the module now:

herong> java --module-path .\cls \
   --module com.herongyang/com.herongyang.util.HelloModularized

Hello world! - Modularized

Of course, I can put my module output files into a JAR file for distribution. See JAR tutorials in this book for more details.

Table of Contents

 About This Book

 Java Tools Terminology

 Java Tools Included in JDK

javac - The Java Program Compiler

 javac - Java Compilation Command and Options

 Compiling Hello.java - My First Java Program

 "javac -classpath" - Specifying Class Path

 "javac -verbose" - Printing Compilation Details

 "javac -sourcepath" - Specifying Source Path

 "javac -d" - Specifying Output Directory

 Two Types of "import" Statements

 "import" Statements Processed by "javac"

 "javac -g" - Controlling Debugging Information

"javac --module" - Compiling Entire Module

 "javac -X" - Specifying Non-Standard Options

 java - The Java Program Launcher

 jar - The JAR File Tool

 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