Java 9 HttpClient java.lang.NoClassDefFoundError: jdk / incubator / http / HttpClient

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 
+5
source share
1 answer

The difference at runtime is the use of the class path and the module path. Application in JUP11 # Incubator modules to notice this as

... incubator modules are not allowed by default for applications on the class path.

therefore, to execute code using the classpath

Applications on the class path should use the --add-modules command line to request permission for the incubator module.


If you want to execute without using the --add-modules option when creating a new module and trying to execute the Main class, make sure that the java command is executed using the module path using the arguments:

 -p or --module-path <module path> 

Searches directories from a list of directories separated by semicolons (;). Each directory is a directory of modules.

 -m or --module <module>/<mainclass 

Specifies the name of the initial module to resolve and, if not specified by the module, then indicates the name of the main class to be executed.

A complete command will be something (for example):

 .../jdk-9.0.1.jdk/Contents/Home/bin/java -p .../jdk9-httpincubate-maven/target/classes -m jdk.httpincubate.maven/http2.Main 

Note

  • The above directory structure is executed in an example project that I created on GitHub to reproduce the same.

  • Just to add to it and without any journalism, I use 2017.3 EAP for intelliJ, and it allows me to Run the main class without arguments of the virtual machine (using the command that includes the arguments as described above.)

+6
source

Source: https://habr.com/ru/post/1273605/


All Articles