Java 9 Modules - Split Packages between Automatic Modules

[Updated: Oct 8, 2017, Created: Oct 8, 2017]

In following example, we will try to split packages between two automatic modules.

Example

We are going to create three projects, two for the automatic modules (having split packages) and one for the application named module.

First non-modular library

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

package parser.xml;

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

Second non-modular library

fast-parser/src/parser/xml/FastParser.java

package parser.xml;

public class FastParser {
  public String parse(String data) {
      String parsedData = XmlParserUtil.parse(data);
      return " parsed by fast parser " + parsedData;
  }
}

Modular Application

parser-app/src/com/example/AppMain.java

package com.example;

import parser.xml.FastParser;

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

parser-app/src/module-info.java

module parser.app {
  requires fast.parser;
}

Compiling and creating Jars

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

Compiling the first non-modular library:

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

Creating jar:

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

Compiling the second non-modular library:

D:\split-packages-between-automatic-modules>mkdir fast-parser\lib
D:\split-packages-between-automatic-modules>copy core-parser.jar fast-parser\lib\core-parser.jar
1 file(s) copied.
D:\split-packages-between-automatic-modules\fast-parser>javac -d out --class-path lib\core-parser.jar src\parser\xml\FastParser.java

Creating Jar

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

Compiling the modular application:

D:\split-packages-between-automatic-modules>mkdir parser-app\lib
D:\split-packages-between-automatic-modules>copy core-parser.jar parser-app\lib\core-parser.jar
1 file(s) copied.
D:\split-packages-between-automatic-modules>copy fast-parser.jar parser-app\lib\fast-parser.jar
1 file(s) copied.
D:\split-packages-between-automatic-modules>tree /A /F
| core-parser.jar
| fast-parser.jar
|
+---core-parser
| +---out
| | \---parser
| | \---xml
| | XmlParserUtil.class
| |
| \---src
| \---parser
| \---xml
| XmlParserUtil.java
|
+---fast-parser
| +---lib
| | core-parser.jar
| |
| +---out
| | \---parser
| | \---xml
| | FastParser.class
| |
| \---src
| \---parser
| \---xml
| FastParser.java
|
\---parser-app
+---lib
| core-parser.jar
| fast-parser.jar
|
\---src
| module-info.java
|
\---com
\---example
AppMain.java
D:\split-packages-between-automatic-modules\parser-app/lib>jar --list --file fast-parser.jar
META-INF/
META-INF/MANIFEST.MF
parser/
parser/xml/
parser/xml/FastParser.class
D:\split-packages-between-automatic-modules\parser-app/lib>jar --list --file core-parser.jar
META-INF/
META-INF/MANIFEST.MF
parser/
parser/xml/
parser/xml/XmlParserUtil.class
D:\split-packages-between-automatic-modules\parser-app>javac --module-path lib -d out src/module-info.java src/com/example/AppMain.java
error: the unnamed module reads package parser.xml from both fast.parser and core.parser
error: module core.parser reads package parser.xml from both fast.parser and core.parser
error: module fast.parser reads package parser.xml from both core.parser and fast.parser
src\module-info.java:1: error: module parser.app reads package parser.xml from both fast.parser and core.parser
module parser.app {
^
4 errors

That verifies that split packages don't work between two automatic modules as well.

Compiling and running as unnamed modules

D:\split-packages-between-automatic-modules\parser-app>javac --class-path lib/core-parser.jar;lib/fast-parser.jar -d out src/com/example/AppMain.java
D:\split-packages-between-automatic-modules\parser-app>java --class-path lib/core-parser.jar;lib/fast-parser.jar;out com.example.AppMain
parsed by fast parser parsed by core util: test data

Example Project

Dependencies and Technologies Used :

  • JDK 9
Package splitting between Automatic Modules Example Select All Download
  • split-packages-between-automatic-modules
    • core-parser
      • src
        • parser
          • xml
    • fast-parser
      • src
        • parser
          • xml
    • parser-app
      • src
        • com
          • example

See Also