Java 9 HttpClient java.lang.NoClassDefFoundError:jdk/incubator/http/HttpClient [英] Java 9 HttpClient java.lang.NoClassDefFoundError: jdk/incubator/http/HttpClient
问题描述
我正在尝试在Java 9 maven项目中使用孵化器中的HttpClient.我没有任何编译问题.该项目成功构建.但是当我尝试运行Main类时,它给了我以下异常:
I am trying to use the HttpClient from incubator in Java 9 maven project. I am not getting any Compilation issue. The project builds 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)....
我的代码只是一个模块信息文件和一个仅调用google.com并尝试读取响应的Main类:
My code is just a module-info file and a Main class that just calls google.com and tries to read the response:
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 this but they are all doing it in this way.
任何帮助都是值得的.谢谢!
Any help is appreicated. Thanks!
使用--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
不使用--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
推荐答案
执行期间的区别是使用类路径与模块路径. JEP11#孵化器模块 中的语句>注意为
The difference during the execution is using the classpath vs the modulepath. The statement in the JEP11#Incubator Modules to notice is as
...默认情况下,不解析孵化器模块的应用程序 类路径.
... incubator modules are not resolved by default for applications on the class path.
所以为了使用类路径执行代码
so in order to execute the code using the classpath
类路径上的应用程序必须使用
--add-modules
命令行选项来请求解决孵化器模块.
Applications on the class path must use the
--add-modules
command-line option to request that an incubator module be resolved.
如果要在不使用--add-modules
选项的情况下执行,则在创建新的module
并尝试执行Main
类时,请确保使用参数使用模块路径执行Java命令:
If you want to execute without using the --add-modules
option, while creating a new module
and trying to execute the Main
class make sure the java command executed using the module path using the arguments :
-p or --module-path <module path>
从以分号(;)的目录列表中搜索目录.每个目录都是模块目录.
Searches for directories from a semicolon-separated (;) list of directories. Each directory is a directory of modules.
-m or --module <module>/<mainclass
指定要解析的初始模块的名称,如果模块未指定,则指定要执行的主类的名称.
Specifies the name of the initial module to resolve and, if it isn’t specified by the module, then specifies the name of the mainclass to execute.
完整的命令应类似于:
.../jdk-9.0.1.jdk/Contents/Home/bin/java
-p .../jdk9-httpincubate-maven/target/classes
-m jdk.httpincubate.maven/http2.Main
注意 :
Note:
-
在我在GitHub上创建的上的示例项目中,遵循上述目录结构./a>也可以复制相同的内容.
The above directory structure is followed in the sample project that I have created on GitHub to replicate the same as well.
只需添加它,无需进行任何公开,我使用的是intelliJ的2017.3 EAP,它可以让我 Run 主类也没有VM参数(使用包含上述共享参数的命令.)
Just to add to it and without any publicisizing I am using 2017.3 EAP of intelliJ and it lets me Run the Main class without the VM arguments as well (using the command including the arguments as shared above.)
这篇关于Java 9 HttpClient java.lang.NoClassDefFoundError:jdk/incubator/http/HttpClient的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!