Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making the module jdk.incubator.httpclient visible at runtime

Question

How can I make the classes from the module jdk.incubator.httpclient visible at runtime?

What I'm Using

Java 9 + Maven + HttpClient jdk.incubator.http.HttpClient

Problems

=> Maven build failing when using jdk.incubator.HttpClient. Fixed with this question thanks to @nullpointer

=> Runtime stacktrace :

java.lang.NoClassDefFoundError: jdk/incubator/http/HttpClient
at com.foo.Bar.Bar.<clinit>(Bar.java:56) ~[?:?]
at java.lang.Class.forName0(java.base@9-Ubuntu/Native Method) ~[?:?]
at java.lang.Class.forName(java.base@9-Ubuntu/Class.java:374) ~[?:?]
Caused by: java.lang.ClassNotFoundException: jdk.incubator.http.HttpClient
at java.net.URLClassLoader.findClass(java.base@9-Ubuntu/URLClassLoader.java:388) ~[?:?]
at java.lang.ClassLoader.loadClass(java.base@9-Ubuntu/ClassLoader.java:486) ~[?:?]
at java.lang.ClassLoader.loadClass(java.base@9-Ubuntu/ClassLoader.java:419) ~[?:?]
at com.foo.Bar.Bar.<clinit>(Bar.java:56) ~[?:?]
at java.lang.Class.forName0(java.base@9-Ubuntu/Native Method) ~[?:?]
at java.lang.Class.forName(java.base@9-Ubuntu/Class.java:374) ~[?:?]

Build Section of Pom

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

    <resources>
        <resource>
            <targetPath>.</targetPath>
            <filtering>true</filtering>
            <directory>${project.basedir}/resources</directory>

            <includes>
                <include>plugin.yml</include>
            </includes>
        </resource>
    </resources>

    <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>

As you can see, I am using the maven-shade-plugin for my dependencies, but since jdk.incubator.http.HttpClient is part of the JDK it is not included in my jar.

Trying to execute as:

 java -jar --add-modules jdk.incubator.httpclient uhc-staging.jar

results into the following exception:

Error occurred during initialization of VM 
java.lang.module.ResolutionException: Module jdk.incubator.httpclient not found 
at java.lang.module.Resolver.fail(java.base@9-Ubuntu/Resolver.java:790) 
at java.lang.module.Resolver.resolveRequires(java.base@9-Ubuntu/Resolver.java:94)
at java.lang.module.Configuration.resolveRequiresAndUses(java.base@9-Ubuntu/Configuration.java:370)
at java.lang.module.ModuleDescriptor$1.resolveRequiresAndUses(java.base@9-Ubuntu/ModuleDescriptor.java:1986)
at jdk.internal.module.ModuleBootstrap.boot(java.base@9-Ubuntu/ModuleBootstrap.java:263)
at java.lang.System.initPhase2(java.base@9-Ubuntu/System.java:1927)
like image 361
Caleb Bassham Avatar asked Nov 23 '17 03:11

Caleb Bassham


1 Answers

In the discussion, the detail brought out was that executing:-

java --list-modules

doesn't include jdk.incubator.httpclient as a module which is the reason why the j.l.m.ResolutionException is thrown. Hence, the solution to this shall be upgrading the JDK version installed to the latest release(which should include the incubator module) and then trying to run the application using the same command as suggested:-

java -jar --add-modules jdk.incubator.httpclient uhc-staging.jar
like image 199
Naman Avatar answered Oct 12 '22 11:10

Naman