Compiling a Module with javac
What You'll Learn
You'll learn how to use the javac compiler to compile modular Java code with the --module-source-path and -d options.
Why Module Compilation is Different
When working with the Java Platform Module System, you're not just compiling individual .java files anymore—you're compiling entire modules with their own boundaries and dependencies. The compiler needs to know where your module sources live and where to put the compiled output, respecting the module structure.
Think of it like organizing a library: instead of throwing all books in one pile, you're sorting them into sections (modules), and the librarian (javac) needs clear instructions about which section's books to process and where to shelve the results.
The Key Compiler Options
--module-source-path
This tells javac where to find your module source directories. Each module's source should be in its own directory, with module-info.java at the root.
Pattern: --module-source-path <base-directory>/* or explicit paths
-d (destination)
This specifies where compiled .class files (and the compiled module-info.class) should go. The compiler preserves the module structure in the output directory.
Basic Compilation Command
javac --module-source-path src -d out --module com.example.mymodule
This compiles the module com.example.mymodule found under src/com.example.mymodule/, placing output in out/com.example.mymodule/.
You can also compile all modules at once:
javac --module-source-path src -d out --module-source-path src/*
Key Takeaway: Use --module-source-path to tell javac where your modules live, and -d to specify the output directory; the compiler maintains module structure in both locations.