Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Incubator Http Classes with Maven in Java9

Tags:

java

maven

java-9

What I am Using

Java 9 + Maven + HttpClient (from java 9) jdk.incubator.http.HttpClient

Problem

When building my project with maven I get the following error

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.7.0:compile (default-compile) on project Core: Compilation failure: Compilation failure:
[ERROR] Foo.java:[4,21] package jdk.incubator.http is not visible

Line 4 of Foo is

import jdk.incubator.http.HttpClient;

Pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<parent>
    <groupId>me.bar</groupId>
    <artifactId>foo</artifactId>
    <version>0.0.0-SNAPSHOT</version>
</parent>

<artifactId>Core</artifactId>

<build>
    <finalName>${project.artifactId}</finalName>
    <sourceDirectory>${project.basedir}/src</sourceDirectory>


    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-clean-plugin</artifactId>
            <version>3.0.0</version>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.7.0</version>
            <configuration>
                <source>9</source>
                <target>9</target>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.1.0</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <minimizeJar>true</minimizeJar>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Note

Sorry, if you think this has an oblivious fix, I am not very good with maven and never have had to deal with the incubator classes before. I have searched for the error but haven't found anything useful. Thanks for the help.

like image 735
Caleb Bassham Avatar asked Nov 23 '17 02:11

Caleb Bassham


1 Answers

While creating a module, you need to create a module-info.java class at the topmost level of your packages which shall thereafter include

module yourModule {
    requires jdk.incubator.httpclient;
}

ensuring that the package jdk.incubator.http exported by the module jdk.incubator.httpclient is visible to your module.

Alternatively, to create a regular classpath application, you can

Compile using:-

<plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.7.0</version>
    <configuration>
        <source>9</source>
        <target>9</target>
        <compilerArgs>
            <arg>--add-modules</arg>
            <arg>jdk.incubator.httpclient</arg>
        </compilerArgs>
    </configuration>
</plugin>

Run using:-

java -jar --add-modules=jdk.incubator.httpclient yourJar.jar
like image 131
Naman Avatar answered Oct 31 '22 12:10

Naman