Java 9 Modules - Split Packages with Automatic Modules

[Updated: Oct 9, 2017, Created: Oct 7, 2017]

In following example, we will try to split packages between a named module (explicitly created) and an Automatic module.

Example

A non-modular library

core-parser/src/parser/xml/XmlParserUtil.java

package parser.xml;

public class XmlParserUtil {
  public static String parse(String data) {
      return "parsed data " + data;
  }
}

A non-modular library (without module-info.java) becomes automatic module when another modular application requires it.

A modular application

parser-app/src/parser/xml/AppMain.java

package parser.xml;

public class AppMain {
  public static void main(String[] args) {
      String data = XmlParserUtil.parse("test data");
      System.out.println(data);
  }
}

parser-app/src/module-info.java

module parser.app {
  requires core.parser;
}

In the requires clause, we used 'core.parser' as the library module name. The name of an automatic module is derived from the jar name (in this example we will create the above library's jar with 'core-parser.jar' name).

Compiling

D:\split-packages-and-automatic-modules>tree /A /F
+---core-parser
| \---src
| \---parser
| \---xml
| XmlParserUtil.java
|
\---parser-app
\---src
| module-info.java
|
\---parser
\---xml
AppMain.java

Compiling the non-modular library

D:\split-packages-and-automatic-modules\core-parser>javac -d out src\parser\xml\XmlParserUtil.java

Creating jar:

D:\split-packages-and-automatic-modules>jar --create --file core-parser.jar -C core-parser/out .

Compiling the modular application

D:\split-packages-and-automatic-modules>mkdir parser-app\lib
D:\split-packages-and-automatic-modules>move core-parser.jar parser-app\lib\core-parser.jar
1 file(s) moved.
D:\split-packages-and-automatic-modules>tree /A /F
+---core-parser
| +---out
| | \---parser
| | \---xml
| | XmlParserUtil.class
| |
| \---src
| \---parser
| \---xml
| XmlParserUtil.java
|
\---parser-app
+---lib
| core-parser.jar
|
\---src
| module-info.java
|
\---parser
\---xml
AppMain.java

D:\split-packages-and-automatic-modules\parser-app>jar --list --file lib\core-parser.jar
META-INF/
META-INF/MANIFEST.MF
parser/
parser/xml/
parser/xml/XmlParserUtil.class
D:\split-packages-and-automatic-modules\parser-app>javac --module-path lib -d out src/module-info.java src/parser/xml/AppMain.java
src\parser\xml\AppMain.java:1: error: package exists in another module: core.parser
package parser.xml;
^
src\parser\xml\AppMain.java:6: error: cannot find symbol
String data = XmlParserUtil.parse("test data");
^
symbol: variable XmlParserUtil
location: class AppMain
2 errors

That verifies that split packages do not work for automatic modules as well.

Compiling and running as unnamed modules

Let's compile parser-app without module-info.java:

D:\split-packages-and-automatic-modules\parser-app>javac --class-path lib/core-parser.jar -d out src/parser/xml/AppMain.java

Running on class path:

D:\split-packages-and-automatic-modules\parser-app>java --class-path lib/core-parser.jar;out parser.xml.AppMain
parsed data test data

Example Project

Dependencies and Technologies Used :

  • JDK 9
Package splitting and Automatic Module Example Select All Download
  • split-packages-and-automatic-modules
    • core-parser
      • src
        • parser
          • xml
    • parser-app
      • src
        • parser
          • xml

See Also