Sub topic 2
Your first module and module-info.java
This chapter builds a first module from scratch and explains the module descriptor that defines it. It assumes you know Java packages and access modifiers, can compile and run programs with javac and java, and understand what a jar file and the classpath are. Basic familiarity with reflection and interfaces helps for a few side notes.
What a module is
A module is a named group of packages, plus a descriptor that states two things:
- which other modules it needs (its dependencies), and
- which of its packages other modules may use (its API).
On the classpath, every public class in every jar is visible to every other class. A module changes that. Code in one module can use another module's types only if the descriptor says so on both sides. The descriptor is a file called module-info.java, and it sits at the root of the module's source tree.
The module descriptor
The descriptor is compiled to module-info.class. Its structure is:
module com.example.greeter {
// directives go here
}Some rules to keep in mind:
- The module name follows package naming conventions. The reverse-domain style (
com.example.greeter) is the norm, and the name should be globally unique. - The name does not have to match a package name, but it usually matches the module's main package prefix.
- The body holds directives such as
requiresandexports. An empty body is legal. A module with no directives exports nothing and reads onlyjava.base. module-info.javais not a normal class.module,requires,exportsand the other directive words are restricted keywords. They have special meaning only inside a module declaration, so existing code that uses them as identifiers still compiles.- There is one descriptor per module, at the root of the source tree for that module.
Building the first module
The example has two modules. The first, com.example.greeter, offers a small API. The second, com.example.app, uses it.
The source layout looks like this:
src/
├── com.example.greeter/
│ ├── module-info.java
│ └── com/example/greeter/api/Greeter.java
└── com.example.app/
├── module-info.java
└── com/example/app/Main.javaEach module gets its own directory, named after the module. This is the layout that the --module-source-path option expects.
The greeter module
// src/com.example.greeter/module-info.java
module com.example.greeter {
exports com.example.greeter.api;
}
// src/com.example.greeter/com/example/greeter/api/Greeter.java
package com.example.greeter.api;
public class Greeter {
public String greet(String name) {
return "Hello, " + name;
}
}The exports directive names a package, not a class and not a module. Only the public types in com.example.greeter.api are accessible to other modules. Any other package inside com.example.greeter stays hidden, even if its classes are public.
The app module
// src/com.example.app/module-info.java
module com.example.app {
requires com.example.greeter;
}
// src/com.example.app/com/example/app/Main.java
package com.example.app;
import com.example.greeter.api.Greeter;
public class Main {
public static void main(String[] args) {
System.out.println(new Greeter().greet("modules"));
}
}The requires directive names a module. It makes the exported packages of com.example.greeter readable by code in com.example.app. Without it, the import line fails to compile, even though the greeter module is on the module path.
Both sides must agree. The provider exports a package, and the consumer requires the provider's module. If either side is missing, the access fails.
Compiling and running
javac -d out --module-source-path src -m com.example.app
java --module-path out -m com.example.app/com.example.app.Main
# Inspect modules
java --list-modules
java --describe-module java.baseHere is what each part does:
--module-source-path srctellsjavacthatsrcholds one directory per module.-m com.example.appcompiles that module and every module it requires that can be found in the source path. Socom.example.greeteris compiled too.-d outwrites the output, one directory per module:out/com.example.appandout/com.example.greeter.--module-path outtells the runtime where to find compiled modules.-m com.example.app/com.example.app.Mainhas the formmodule/main-class. The part before the slash is the module, and the part after is the fully qualified class name.
The program prints Hello, modules.
java --list-modules prints the modules in the runtime image, such as java.base and java.sql. java --describe-module java.base prints a module's descriptor: its exports, its requirements and its services.
The next diagram shows how the compiler and runtime see the two modules.
Everything requires java.base
You never write requires java.base. The compiler adds it to every module automatically. It holds the core packages such as java.lang, java.util and java.io. This is why System.out.println works in the app module without any declaration.
Other JDK modules are not implicit. If your code uses java.sql.Connection, you must write requires java.sql;. Later chapters cover the modular JDK in more detail.
Common errors
The compiler reports module problems with specific messages. These are the ones a new module author meets first.
| Message | Cause | Fix |
|---|---|---|
| `package com.example.greeter.api is not visible` | The app module lacks `requires com.example.greeter`, or the greeter module does not export that package. | Add the missing `requires` or `exports`. The detail line after the message says which one is missing. |
| `module not found: com.example.greeter` | The required module is not in the module source path or module path. | Check the directory names and the `--module-source-path` or `--module-path` value. |
| `module name com.example.app does not match expected name` | The directory name differs from the name in `module-info.java`. | Rename one so they match. |
| `Module com.example.app not found` at run time | `--module-path` does not contain the compiled module. | Point `--module-path` at the output directory. |
Design habits from the start
- Keep one module per directory and name the directory after the module. It avoids path errors.
- Export as little as possible. Putting types in an
apipackage and everything else ininternalorimplpackages makes the exported surface obvious. - Do not export a package just to make a compile error go away. First ask whether the consumer should depend on that type at all.
- Treat the module name as part of your public contract. Renaming it later breaks every
requiresthat mentions it.
Exercises
Exercise
The app module imports com.example.greeter.api.Greeter and its descriptor contains requires com.example.greeter;. Compilation still fails with "package com.example.greeter.api is not visible". What is the most likely cause, and what is the fix?
Show answer
The greeter module does not export that package. The requires directive only grants access to packages the other module exports. Add exports com.example.greeter.api; to the greeter module's module-info.java.
Exercise
In the command java --module-path out -m com.example.app/com.example.app.Main, what do the two parts around the slash mean, and why does the module name appear at all when the class name is fully qualified?
Show answer
The part before the slash is the module that contains the main class, and the part after is the fully qualified class name. The runtime uses the module name as the root of the module graph, so it knows which module to resolve first and which of its dependencies to load.
Exercise
Why does System.out.println work in the app module even though its descriptor never mentions java.base?
Show answer
Every module implicitly requires java.base, and java.lang.System is in an exported package of that module. The compiler adds the dependency automatically.