简体   繁体   English

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

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

I am trying to use the HttpClient from incubator in Java 9 maven project. 我正在尝试使用Java 9 maven项目中的孵化器中的HttpClient。 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: 但是当我尝试运行Main类时,它给了我以下异常:

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-info file and a Main class that just calls google.com and tries to read the response: 我的代码只是一个模块信息文件和一个只调用google.com并试图读取响应的Main类:

module-info.java module-info.java

module java9.http_client {   
    requires jdk.incubator.httpclient;
}

Main.java 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 的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! 谢谢!

Command executed when using --add-modules 使用--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

When not using --add-modules 不使用--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

The difference during the execution is using the classpath vs the modulepath. 执行期间的差异是使用类路径与模块路径。 The statement in the JEP11#Incubator Modules to notice is as 注意JEP11#Incubator Modules中的声明为

... incubator modules are not resolved by default for applications on the class path. ...默认情况下,类路径上的应用程序不会解析孵化器模块。

so in order to execute the code using the classpath 所以为了使用类路径执行代码

Applications on the class path must use the --add-modules command-line option to request that an incubator module be resolved. 类路径上的应用程序必须使用--add-modules命令行选项来请求解析孵化器模块。


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 : 如果要在不使用--add-modules选项的情况下执行,则在创建新module并尝试执行Main类时,请确保使用参数使用模块路径执行java命令:

-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. 指定要解析的初始模块的名称,如果模块未指定,则指定要执行的主类的名称。

The complete command would be something (eg) like : 完整的命令将是(例如)像:

.../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 followed in the sample project that I have created on GitHub to replicate the same as well. 在GitHub上创建的示例项目中遵循了上述目录结构,以便复制相同的目录结构。

  • 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.) 只是添加它并且没有任何公开化我使用2017年的IntelliJ EAP并且它允许我在没有VM参数的情况下运行 Main类(使用包括上面共享的参数的命令。)

If the accepted answer still did not work for you then.. 如果接受的答案仍然不适合你那么..

The missing secret is to also include the maven-surefire-plugin with the same compiler arg. 缺少的秘密还包括使用相同编译器arg的maven-surefire-plugin

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.7.0</version>
                <configuration>
                    <source>10</source>
                    <target>10</target>
                    <compilerArgument>--add-modules=jdk.incubator.httpclient</compilerArgument>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.21.0</version>
                <configuration>
                    <argLine>--add-modules=jdk.incubator.httpclient</argLine>
                </configuration>
            </plugin>
        </plugins>
    </build>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java和httpclient java.lang.NoClassDefFoundError - java and httpclient java.lang.NoClassDefFoundError java.lang.NoClassDefFoundError: org/apache/http/client/HttpClient - java.lang.NoClassDefFoundError: org/apache/http/client/HttpClient 处理程序调度失败; 嵌套的异常是java.lang.NoClassDefFoundError:org / apache / http / client / HttpClient - Handler dispatch failed; nested exception is java.lang.NoClassDefFoundError: org/apache/http/client/HttpClient java.lang.NoClassDefFoundError: org/apache/commons/httpclient/Credentials 错误 - java.lang.NoClassDefFoundError: org/apache/commons/httpclient/Credentials error 线程“main”中的异常 java.lang.NoClassDefFoundError: org/apache/http/client/HttpClient - Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/http/client/HttpClient Java11中找不到jdk.incubator.httpclient模块 - The jdk.incubator.httpclient module not found in Java11 SpringBoot 3 与 Spring Cloud Vault 3.1.1 运行时错误 java.lang.NoClassDefFoundError: org/apache/hc/client5/http/classic/HttpClient - SpringBoot 3 with Spring Cloud Vault 3.1.1 Runtime Error java.lang.NoClassDefFoundError: org/apache/hc/client5/http/classic/HttpClient jdk.incubator.httpclient中的NullPointerException - NullPointerException in jdk.incubator.httpclient WildFly服务模块加载程序-jdk.incubator.http.HttpClient上的ClassNotFound - WildFly Service Module Loader - ClassNotFound on jdk.incubator.http.HttpClient HttpClient java NoClassDefFoundError - HttpClient java NoClassDefFoundError
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM