Close

Java 9 Modules - Developing Java 9 Modules with Apache Maven

[Last Updated: Sep 5, 2018]

Following example shows how to develop Java 9 Modules using Maven (version 3.5.4).

Example

In this example we are going to create a simple Java 9 module. We are also going to use Jsoup (version 1.11.3) as an external library which is not modular, so we need to add that as an automatic module.

Pom file

pom.xml

<project .....>
<modelVersion>4.0.0</modelVersion>

<groupId>com.logicbig.example</groupId>
<artifactId>java9-module-and-apache-maven</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.11.3</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>10</source>
<target>10</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>

module-info.java

java9-module-and-apache-maven/src/main/java/module-info.java

module example.module {
  requires org.jsoup;
}

We found Jsoup automatic module name by using --describe-module of 'jar' command:

C:\Users\Joe\.m2\repository\org\jsoup\jsoup\1.11.3>jar --file=jsoup-1.11.3.jar --describe-module
No module descriptor found. Derived automatic module.

org.jsoup@1.11.3 automatic
requires java.base mandated
contains org.jsoup
contains org.jsoup.helper
contains org.jsoup.internal
contains org.jsoup.nodes
contains org.jsoup.parser
contains org.jsoup.safety
contains org.jsoup.select

Jsoup jar's MANIFEST.MF has an entry Automatic-Module-Name: org.jsoup (you have to extract the jar to see that). Check out how automatic module names are derived at the end of Java 9 Automatic Modules tutorial.

Main class

java9-module-and-apache-maven/src/main/java/com/logicbig/example/ExampleMain.java

package com.logicbig.example;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

public class ExampleMain {

  public static void main(String[] args) {
      Document d = Jsoup.parse("<a href='http://www.example.com'>Example</a>");
      String s = d.getElementsByTag("a")
                     .first()
                     .attr("href");
      System.out.println(s);
  }
}
http://www.example.com

Above output is taken from Intellij which integrates with maven and runs the following command (you can find that in the Run view, shortcut Alt+4):

D:\java\jdk10\bin\java.exe "-javaagent:D:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2018.1\lib\idea_rt.jar=59186:D:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2018.1\bin" -Dfile.encoding=UTF-8 -p D:\java9-module-and-apache-maven\target\classes;C:\Users\Joe\.m2\repository\org\jsoup\jsoup\1.11.3\jsoup-1.11.3.jar -m example.module/com.logicbig.example.ExampleMain

Intellij also has autocompletion support for automatic module names (even if it's maven project):

Running via maven exec:java

D:\java9-module-and-apache-maven>mvn -q clean compile exec:java -Dexec.mainClass="com.logicbig.example.ExampleMain"
http://www.example.com

If not requiring jsoup, i.e.

module example.module {
  // requires org.jsoup;
}

then output will be:

D:\java9-module-and-apache-maven>mvn -q clean compile exec:java -Dexec.mainClass="com.logicbig.example.ExampleMain"
[ERROR] COMPILATION ERROR :
[ERROR] /D:/java9-module-and-apache-maven/src/main/java/com/logicbig/example/ExampleMain.java:[3,17] package org.jsoup does not exist
[ERROR] /D:/java9-module-and-apache-maven/src/main/java/com/logicbig/example/ExampleMain.java:[4,23] package org.jsoup.nodes does not exist
[ERROR] /D:/java9-module-and-apache-maven/src/main/java/com/logicbig/example/ExampleMain.java:[9,9] cannot find symbol
symbol: class Document
location: class com.logicbig.example.ExampleMain
[ERROR] /D:/java9-module-and-apache-maven/src/main/java/com/logicbig/example/ExampleMain.java:[9,22] cannot find symbol
symbol: variable Jsoup
location: class com.logicbig.example.ExampleMain
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.0:compile (default-compile) on project java9-module-and-apache-maven: Compilation failure: Compilation failure:
[ERROR] /D:/java9-module-and-apache-maven/src/main/java/com/logicbig/example/ExampleMain.java:[3,17] package org.jsoup does not exist
[ERROR] /D:/java9-module-and-apache-maven/src/main/java/com/logicbig/example/ExampleMain.java:[4,23] package org.jsoup.nodes does not exist
[ERROR] /D:/java9-module-and-apache-maven/src/main/java/com/logicbig/example/ExampleMain.java:[9,9] cannot find symbol
[ERROR] symbol: class Document
[ERROR] location: class com.logicbig.example.ExampleMain
[ERROR] /D:/java9-module-and-apache-maven/src/main/java/com/logicbig/example/ExampleMain.java:[9,22] cannot find symbol
[ERROR] symbol: variable Jsoup
[ERROR] location: class com.logicbig.example.ExampleMain
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

Running via java command

D:\java9-module-and-apache-maven\target>java --module-path classes;C:\Users\Joe\.m2\repository\org\jsoup\jsoup\1.11.3 --module example.module/com.logicbig.example.ExampleMain
http://www.example.com

Example Project

Dependencies and Technologies Used:

  • jsoup 1.11.3: jsoup is a Java library for working with real-world HTML. It provides a very convenient API for extracting and manipulating data, using the best of DOM, CSS, and jquery-like methods. jsoup implements the WHATWG HTML5 specification, and parses HTML to the same DOM as modern browsers do. [Description from jsoup-1.11.3.pom]
  • JDK 10
  • Maven 3.5.4

Developing Java 9 Module with Apache Maven Example Select All Download
  • java9-module-and-apache-maven
    • src
      • main
        • java
          • com
            • logicbig
              • example
          • module-info.java

    See Also