Automatic Modules in Java

You now know to put the Java Platform Module System to use. If you start with a brand-new project in which you write all the code yourself, you can design modules, declare module dependencies, and package your application into modular JAR files.

However, that is an extremely uncommon scenario. Almost all projects rely on third-party libraries. Of course, you can wait until the providers of all li­braries have turned them into modules, and then modularize your own code.

But what if you don’t want to wait? The Java Platform Module System provides two mechanisms for crossing the chasm that separates today’s premodular world and fully modular applications: automatic modules and the unnamed module.

For migration purposes, you can turn any JAR file into a module simply by placing it onto a directory in the module path instead of the class path. A JAR without a module-info.class on the module path is called an automatic module. An automatic module has the following properties:

  1. The module implicitly has a requires clause for all other modules.
  2. All of its packages are exported and opened.
  3. If there is an entry with key Automatic-Module-Name in the JAR file manifest META-INF/MANIFEST.MF, its value becomes the module name.
  4. Otherwise the module name is obtained from the JAR file name, dropping any trailing version number and replacing sequences of non-alphanumeric characters with a dot.

The first two rules imply that the packages in the automatic module act as if they were on the class path. The reason for using the module path is for the benefit of other modules, allowing them to express dependencies on this module.

Suppose, for example, that you are implementing a module that processes CSV files and uses the Apache Commons CSV library. You would like to express in your module-info.java file that your module depends on Apache Commons CSV.

If you add commons-csv-1.5.jar onto the module path, then your modules can reference the module. Its name is commons.csv since the trailing version number -1.5 is removed and the non-alphanumeric character – is replaced by a dot.

This name might be an acceptable module name because Commons CSV is well known and it is unlikely that someone else will try to use the same name for a different module. But it would be better if the maintainers of this JAR file could quickly agree to reserve a reverse DNS name, preferably the top- level package name org.apache.commons.csv, as the module name. They just need to add a line

Automatic-Module-Name: org.apache.commons.csv

to the META-INF/MANIFEST.MF file inside the JAR. Eventually, hopefully, they will turn the JAR file into a true module by adding module-info.java with the reserved module name—and every other module that refers to the CSV module with that name will just continue to work.

As this book is being written, version 1.5 of the Commons CSV JAR file does not have a module descriptor or an automatic module name. Nevertheless, it will work fine on the module path. You can download the library from https://commons.apache.org/proper/commons-csv. Uncompress and place the commons-csv-1.5.jar file into the directory of the v2ch9.automod module. That module contains a simple program that reads a CSV file with country data:

package com.horstmann.places;

import java.io.*;

import org.apache.commons.csv.*;

public class CSVDemo

{

public static void main(String[] args) throws IOException

{

var in = new FileReader(“countries.csv”);

Iterable<CSVRecord> records = CSVFormat.EXCEL.withDelimiter(Y)

.withHeader().parse(in);

for (CSVRecord record : records)

{

String name = record.get(“Name”);

double area = Double.parseDouble(record.get(“Area”));

System.out.println(name + ” has area ” + area);

}

}

}

 

Since we will use commons-csv-1.5.jar as an automatic module, we need to require it:

@SuppressWarnings(“module”) module v2ch09.automod

{

requires commons.csv;

}

Here are the commands for compiling and running the program:

javac -p v2ch09.automod:commons-csv-1.5.jar \

v2ch09.automod/com/horstmann/places/CSVDemo.java \

v2ch09.automod/module-info.java

java -p v2ch09.automod:commons-csv-1.5.jar \

-m v2ch09.automod/com.horstmann.places.CSVDemo

Source: Horstmann Cay S. (2019), Core Java. Volume II – Advanced Features, Pearson; 11th edition.

Leave a Reply

Your email address will not be published. Required fields are marked *