Sub topic 14
JAR Files and a First Look at Modules
This chapter assumes you can declare packages, compile Java classes, and run a class with java. It introduces two ways to distribute packaged code: JAR files and Java modules.
What a JAR file contains
A JAR, or Java Archive, is a ZIP-format file designed for Java applications and libraries. It can contain compiled .class files, package directories, resources, and metadata.
A JAR does not replace packages. The package structure remains inside the archive:
shop.jar
├── META-INF/
└── com/
└── example/
└── shop/
├── Main.class
└── model/
└── Product.classThe fully qualified name of Main is still com.example.shop.Main. The JAR only changes how the files are stored and distributed.
Creating and using a JAR
First compile the source files into an output directory:
javac -d out \
src/com/example/shop/Main.java \
src/com/example/shop/model/Product.javaCreate a JAR from the contents of out:
jar --create --file shop.jar -C out .The -C out . part tells jar to enter out and archive everything below it. The directory itself is not added as an extra path component.
Run the packaged application by placing the JAR on the classpath:
java -cp shop.jar com.example.shop.MainThe classpath is the set of locations where the Java compiler and launcher search for classes. A classpath can contain directories, JAR files, or both.
A JAR can also declare a main class in its manifest. Then you can run it with java -jar:
jar --create --file shop-app.jar --main-class com.example.shop.Main -C out .
java -jar shop-app.jarThis form is convenient for applications. Libraries are usually placed on another program's classpath instead.
Modules add explicit boundaries
A module is a named group of packages with declared dependencies and exports. Modules were introduced in Java 9 to make large applications more reliable and maintainable.
A module descriptor is a file named module-info.java:
module com.example.shop {
exports com.example.shop;
requires java.logging;
}This declaration says:
- The module is named
com.example.shop. - Code in other modules may use public types in
com.example.shop. - The module requires the standard
java.loggingmodule.
A public class in a package that is not exported is still hidden from code in other modules. Therefore, public controls class-level access, while exports controls module-level visibility.
The java.base module is available automatically. It contains fundamental packages such as java.lang and java.util.
Classpath and module path
A traditional JAR is commonly used on the classpath:
java -cp shop.jar com.example.shop.MainA modular JAR is commonly used on the module path:
java --module-path shop.jar --module com.example.shop/com.example.shop.MainThe module path lets Java read module names, required modules, and exported packages. A JAR without module-info.class can still be used on the classpath. When placed on the module path, it may be treated as an automatic module, but its inferred name and broad visibility can be less predictable than an explicitly named module.
Choosing between JARs and modules
Use a plain JAR when you need simple distribution or compatibility with older classpath-based tools. Use a modular JAR when an application has clear component boundaries, needs reliable dependency declarations, or benefits from strong encapsulation.
Modules do not eliminate packages or imports. Java still resolves types through package names and imports. Modules add another check around those packages.
Exercise
Why might a public class in a modular JAR be inaccessible to another module?
Show answer
Its package may not be listed in the module's exports declaration, or the using module may not require the module that contains it.