module-info.java
Declare a module.
module-info.java is a free Java Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
The Module System
The Java Platform Module System (JPMS), introduced in Java 9, lets you group packages into named modules with explicit dependencies and boundaries.
The declaration lives in a special file: module-info.java.
Why Modules
Before modules, the classpath was a flat, unstructured bag of JARs. Anyone could access any public class, and missing dependencies surfaced only at runtime.
Modules add strong encapsulation and reliable configuration verified at compile and launch time.
Where module-info Lives
module-info.java sits at the root of your source tree, above your package directories. It compiles into a module-info.class at the root of the module.
There is exactly one per module.
Minimal Declaration
The simplest module just has a name. By convention names use reverse-DNS, like a package.
module com.example.app {
}Declaring a Dependency
Use requires to depend on another module. Every module implicitly requires java.base, so you never declare that one.
module com.example.app {
requires java.sql;
requires java.logging;
}Exposing a Package
By default a module's packages are hidden. Use exports to make a package's public types visible to other modules.
module com.example.app {
exports com.example.app.api;
}Strong Encapsulation
Any package you do not export is completely inaccessible from outside, even its public classes. This is the big difference from the classpath world.
Internal packages stay internal, enforced by the JVM.
Module Naming Rules
Module names follow the same lexical rules as packages: dot-separated identifiers. Choose a stable, globally unique name because other modules depend on it by name.
Renaming a module is a breaking change for its consumers.
A Fuller Example
A real declaration combines requires and exports. This module depends on logging and exposes only its public API package.
module com.example.orders {
requires java.logging;
requires com.example.payments;
exports com.example.orders.api;
}Compiling a Module
You compile with the module-aware flags. The --module-source-path and -d options tell the compiler where sources and output live.
javac -d out --module-source-path src $(find src -name '*.java')java --module-path out --module com.example.app/com.example.app.Main
The Module Path
Modules are resolved from the module path (--module-path), the modular successor to the classpath. The launcher reads each module's descriptor to build a dependency graph before running.
Quick Check
Recall the default visibility of packages.
Recap
You learned the basics of module-info.java:
- One descriptor per module at the source root
requiresdeclares dependencies;java.baseis implicitexportsexposes a package; everything else is encapsulated- Built and run with
--module-pathand--module
Next: requires and exports directives in depth.
Frequently asked questions
Is the “module-info.java” lesson free?
Yes — the full text of “module-info.java” is free to read here on the web, and the Java Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.
What will I learn in “module-info.java”?
Declare a module. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Java Academy?
No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “module-info.java” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Java Academy lesson?
Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.