当前位置: 首页 > 知识库问答 >
问题:

Java9 HttpClientjava.lang.NoClassDefFoundError: jdk/孵化器/超文本传输协议/HttpClient

段溪叠
2023-03-14

我试图在Java9Maven项目中使用孵化器中的HttpClient。我没有收到任何编译问题。项目成功构建。但是,当我尝试运行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)....

我的代码只是一个模块信息文件和一个调用google的主类。com并尝试读取响应:

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);
    }
}
}

波姆。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>

我错过了什么?我找到了很多关于这方面的文章,但他们都是这样做的。

任何帮助都是需要的。谢谢!

使用--add模块时执行的命令

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

不使用时--添加模块

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

共有2个答案

鲁淇
2023-03-14

如果接受的答案仍然不适用于您,那么。。

缺少的秘密还包括使用相同编译器arg的maven-surefire插件

<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>
宫瀚
2023-03-14

执行过程中的不同之处在于使用类路径和模块路径。JEP11#孵化器模块中需要注意的语句如下

... 默认情况下,类路径上的应用程序不会解析孵化器模块。

为了使用类路径执行代码

类路径上的应用程序必须使用--add-模块命令行选项来请求解析孵化器模块。

如果要在不使用--addmodules选项的情况下执行,则在创建新的模块并尝试执行Main类时,请确保使用模块路径并使用参数执行java命令:

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

从分号(;)中搜索目录目录列表。每个目录都是一个模块目录。

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

指定要解析的初始模块的名称,如果模块未指定该名称,则指定要执行的主类的名称。

完整的命令应该是这样的:

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

注:

>

  • 上面的目录结构在我在GitHub上创建的示例项目中也遵循,以复制相同的目录结构。

    只是为了添加它,而不进行任何公开,我正在使用intelliJ的2017.3 EAP,它允许我在不使用VM参数的情况下运行主类(使用包含上述共享参数的命令)

  •  类似资料:
    • httpd是Apache超文本传输协议(HTTP)服务器的主程序。被设计为一个独立运行的后台进程,它会建立一个处理请求的子进程或线程的池。 通常,httpd不应该被直接调用,而应该在类Unix系统中由apachectl调用,在Windows NT/2000/XP/2003中作为服务运行和在Windows 95/98/ME中作为控制台程序运行. 语法 httpd [ -d serverroot ]

    • 我正在使用GWT和Spring controller来管理http流量。有些请求可能需要很长时间,但我希望在超过给定时间时终止请求。 我如何配置超时Spring。我也使用Apache Tomcat 7.0。我试图在tomcat上inrease最大线程,但有一段时间tomcat工作缓慢,因为请求线程不会死。

    • 我只是有一个关于服务中http请求的结构和处理响应的问题。我正在使用Angular2。alpha46 Typescript(刚刚开始测试-我喜欢它…Ps…。感谢所有一直致力于它并通过github作出贡献的人) 因此,采取以下措施: 登录表单。组成部分ts 从这个组件中,我导入了我的userService,它将容纳我的超文本传输协议请求,以登录用户。 使用者服务ts 我想做的是能够处理http请求之

    • 我的LogCat: 签名密钥(sw)为https://api.dropbox.com/1/shares/dropbox/a.jpg?oauth_consumer_key=2f2y1dyuqhp58ek 我对http没有太多经验。。 因为httpPost=新的httpPost(sw);工作正常,这是否意味着基本字符串签名正确? 还是我错过了什么?

    • 我试图禁用我的AngularJS应用程序中的缓存,但它无法使用以下代码: 当我使用

    • 我需要检查与vbscript的http连接 我想打给主机看看主机是否有反应 我需要测试到特定端口的连接,为什么不使用url呢 你有解决办法吗?