I am trying to use HttpClient from an incubator in a Java 9 maven project. I have no compilation. The project is working successfully. But when I try to run the Main class, it gives me the following exception:
Exception in thread "main" java.lang.NoClassDefFoundError: jdk/incubator/http/HttpClient at java9.http_client.Main.main(Main.java:18) Caused by: java.lang.ClassNotFoundException: jdk.incubator.http.HttpClient at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:582) at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:185) at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:496)....
My code is just a module information file and a Main class that simply calls google.com and tries to read the answer:
module-info.java
module java9.http_client { requires jdk.incubator.httpclient; }
Main.java
public final class Main { public static void main(String[] args) { try { HttpClient client = HttpClient.newHttpClient(); URI httpURI = new URI("http://www.google.com/"); HttpRequest request = HttpRequest.newBuilder(httpURI).GET().build(); HttpResponse<String> response = client.send(request, HttpResponse.BodyHandler.asString()); String responseBody = response.body(); int responseStatusCode = response.statusCode(); System.out.println(responseBody + "\n" + responseStatusCode); } catch (URISyntaxException | IOException | InterruptedException e) { throw new RuntimeException("Unable to run Java 9 Http Client examples", e); } } }
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.javacodegeeks.java9</groupId> <artifactId>http_client</artifactId> <version>0.0.1-SNAPSHOT</version> <name>Java9HttpClient</name> <description>Java Http Client example</description> <properties> <java-version>1.9</java-version> <maven-compiler-plugin-version>3.6.1</maven-compiler-plugin-version> <maven-shade-plugin-version>3.0.0</maven-shade-plugin-version> </properties> <build> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>${maven-compiler-plugin-version}</version> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>${maven-shade-plugin-version}</version> </plugin> </plugins> </pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>${java-version}</source> <target>${java-version}</target> <verbose>true</verbose> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>com.javacodegeeks.java9.http_client.Main</mainClass> </transformer> </transformers> </configuration> </execution> </executions> </plugin> </plugins> </build>
What am I missing? I found various articles about it, but they all do it that way.
Any help could be used. Thanks!
Command executed using -add-modules
cd /home/mansi/NetBeansProjects/java9-http-client; JAVA_HOME=/home/mansi/jdk-9 /home/mansi/netbeans-dev-201709070001/java/maven/bin/mvn "-Dexec.args=--add-modules=jdk.incubator.httpclient -classpath %classpath java9.http_client.Main" -Dexec.executable=/home/mansi/jdk-9/bin/java -Dexec.classpathScope=runtime org.codehaus.mojo:exec-maven-plugin:1.5.0:exec
If not use -add-modules
cd /home/mansi/NetBeansProjects/java9-http-client; JAVA_HOME=/home/mansi/jdk-9 /home/mansi/netbeans-dev-201709070001/java/maven/bin/mvn "-Dexec.args=-classpath %classpath java9.http_client.Main" -Dexec.executable=/home/mansi/jdk-9/bin/java -Dexec.classpathScope=runtime org.codehaus.mojo:exec-maven-plugin:1.5.0:exec
source share