Sub topic 2
Your first module and `module-info.java`
Assume you know Java packages, access modifiers, javac, java, JAR files, and the classpath. This chapter uses Java 12 syntax and commands.
A module is a named group of packages and resources. A module declares its name, the modules it needs, and the packages it makes available. The declaration is stored in a file named module-info.java.
A module descriptor
Create this source tree:
src/
└── com.example.greeter/
├── module-info.java
└── com/example/greeter/api/Greeter.javaThe directory directly below src is the module's source root. Its name matches the module name.
// src/com.example.greeter/module-info.java
module com.example.greeter {
exports com.example.greeter.api;
}The module declaration gives the module its name. The exports directive makes one package available to other modules. Only the exported package is part of the module's supported external API.
// src/com.example.greeter/api/Greeter.java
package com.example.greeter.api;
public class Greeter {
public String greet(String name) {
return "Hello, " + name;
}
}A public class is accessible outside its module only when its package is exported. public still matters, but it is no longer sufficient by itself.
Compile and inspect the module
Compile the module into an output directory:
javac -d out --module-source-path src -m com.example.greeterHere, --module-source-path tells javac where to find module source roots. The -m option selects the module to compile.
You can inspect the descriptor with javap:
javap --module-path out --module com.example.greeterThe output includes the module name and its exported package.
| Descriptor element | Meaning |
|---|---|
| `module com.example.greeter` | Declares a named module |
| `exports com.example.greeter.api` | Makes that package accessible to readable modules |
| No `requires` directive | The module still reads `java.base` implicitly |
Every Java module implicitly depends on java.base. Classes such as String and System therefore need no explicit requires java.base; declaration.
The module boundary
The module system checks two separate conditions:
- The consumer module must read the provider module through
requires. - The provider module must export the package containing the used type.
This creates an explicit boundary around implementation packages. A package that is not exported can contain public classes, but code in another named module cannot use those classes directly.
flowchart TD
A[module-info.java] --> B[Named module]
B --> C[Exports selected packages]
C --> D[Other modules can use exported types]
B --> E[Unexported packages remain internal]Common errors
- Naming the directory differently from the module declaration can make source discovery fail.
- Putting
module-info.javainside a package directory givesjavacan invalid module layout. - Forgetting
exportscauses an access error when another named module imports the package. - Compiling a modular application on the classpath hides module-boundary problems. Use the module path when testing modules.
Exercise
Why can another named module fail to use a public class in com.example.greeter.internal?
Show answer
The class's package is not exported by com.example.greeter, so the other module cannot access it across the module boundary.