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

JDK9上的“package java.net.http dot not exist”错误

梅庆
2023-03-14

我在从HttpRequest Javadoc编译简单的阻塞GET示例时遇到问题:

package org.example;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

import static java.net.http.HttpRequest.noBody;
import static java.net.http.HttpResponse.asString;

public class Http2 {
    public static void main(String[] args) throws URISyntaxException, IOException, InterruptedException {
        HttpResponse response = HttpRequest
                .create(new URI("http://www.infoq.com"))
                .body(noBody())
                .GET().response();
        int responseCode = response.statusCode();
        String responseBody = response.body(asString());

        System.out.println(responseBody);
    }
}
{ jdk9 }  » /cygdrive/c/Program\ Files/Java/jdk-9/bin/javac -d out/production -modulesourcepath org.example.module1/src/ -m org.example.module1

org.example.module1\src\org.example.module1\org\example\Http2.java:6: error: package java.net.http does not exist
import java.net.http.HttpRequest;
                    ^
org.example.module1\src\org.example.module1\org\example\Http2.java:7: error: package java.net.http does not exist
import java.net.http.HttpResponse;
                    ^
org.example.module1\src\org.example.module1\org\example\Http2.java:9: error: package java.net.http does not exist
import static java.net.http.HttpRequest.noBody;
                           ^
org.example.module1\src\org.example.module1\org\example\Http2.java:9: error: static import only from classes and interfaces
import static java.net.http.HttpRequest.noBody;
^
org.example.module1\src\org.example.module1\org\example\Http2.java:10: error: package java.net.http does not exist
import static java.net.http.HttpResponse.asString;
                           ^
org.example.module1\src\org.example.module1\org\example\Http2.java:10: error: static import only from classes and interfaces
import static java.net.http.HttpResponse.asString;
^
org.example.module1\src\org.example.module1\org\example\Http2.java:14: error: cannot find symbol
        HttpResponse response = HttpRequest
        ^
  symbol:   class HttpResponse
  location: class Http2
org.example.module1\src\org.example.module1\org\example\Http2.java:14: error: cannot find symbol
        HttpResponse response = HttpRequest
                                ^
  symbol:   variable HttpRequest
  location: class Http2
org.example.module1\src\org.example.module1\org\example\Http2.java:16: error: cannot find symbol
                .body(noBody())
                      ^
  symbol:   method noBody()
  location: class Http2
org.example.module1\src\org.example.module1\org\example\Http2.java:19: error: cannot find symbol
        String responseBody = response.body(asString());
                                            ^
  symbol:   method asString()
  location: class Http2
10 errors

使用命令行和intellij也会出现相同的错误。

对于我的模块来说,这不是问题,因为没有java.net.http的类编译和运行没有任何问题。

知道这是怎么回事吗?

共有1个答案

陶瀚玥
2023-03-14

src/org/example/module-info.java中的模块定义(基于包名)中,您需要将依赖项添加到java.net.http包中,该包包含在java.httpclient模块中:

module org.example {
    requires java.httpclient;
}

您可以在模块摘要中找到JDK模块的列表。

 类似资料:
  • 我在编译HttpRequest Javadoc中的简单块GET示例时遇到了一个问题: 使用JDK 9编译时,我得到错误: 使用命令行和Intellij也会出现相同的错误。 我的模块没有问题,因为没有java.net.http的类编译和运行没有任何问题。 你知道怎么回事吗?

  • 刚开始玩jdk9 - 并且有点卡在一开始: < li >下载并解压缩了eclipse-Java-neon-m4a-win32 < li >已安装java 9支持(通过市场中的免打扰) < li >下载并安装了jdk9u99(只有jdk,没有公共jre) 此时,我可以在默认的Java(8u 60)上运行eclipse,既可以使用JRE/JDK(eclipse . ini中的/out vm参数),也可

  • 今天,我尝试了最新的jdk9版本113,并尝试使用最新的Maven 3.3.9编译我的项目 这些是命令(部分通过twitter找到) 但是我得到这个错误...总之: JDK9 的正确 maven 配置或参数是什么?

  • 问题内容: 我刚刚尝试发现它现在不包含本机方法,而是将它们委托给some ,例如: 反过来,最新的实际上看起来像旧的,但是现在这些方法都带有一些注释: 那么,使用启动JDK9是否“安全” ?现在是公共官方API吗? 问题答案: 这是一个很好的解释: https://adtmag.com/blogs/watersworks/2015/08/java-9-hack.aspx 在Java 9中如何处理s

  • 问题内容: 通过阅读这个问题,我发现JDK9不可变集和映射将引入随机性源,这将影响它们的遍历。这意味着,至少在JVM的不同运行之间, 迭代顺序 确实是随机的。 由于规范不保证集合和地图的遍历/迭代顺序,因此绝对可以。实际上,代码绝不能依赖于实现特定的细节,而只能依赖规范。 我知道今天使用JDK 8,如果我拥有ie 并执行此操作(摘自链接的答案): 然后,元素的 迭代顺序 将更改,并且两个输出将不同

  • 我犯了错误: 有人有什么想法吗?多谢!