JDK9 上的“包 java.net.http 不存在”错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36910746/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 02:00:03  来源:igfitidea点击:

"package java.net.http does not exist" error on JDK9

javajava-9

提问by ?ukasz Koniecki

I have a problem compiling simple blocking GET example from the HttpRequest JavaDoc:

我在从 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);
    }
}

I'm getting package java.net.http does not existerror when compiling using JDK 9:

我得到package java.net.http does not exist使用JDK 9编译时错误:

{ 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

Same error occurs using command line and IntelliJ.

使用命令行和 IntelliJ 会发生同样的错误。

It is not a problem with my module because classes without java.net.http compiles and run without any problem.

我的模块没有问题,因为没有 java.net.http 的类可以毫无问题地编译和运行。

Any idea what is going on?

知道发生了什么吗?

采纳答案by assylias

In your module definition, located (based on your package name) in src/org/example/module-info.java, you need to add the dependency to the java.net.httppackage, which is included in the java.httpclientmodule:

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

module org.example {
    requires java.httpclient;
}

You can find the list of JDK modules in the module summary.

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

回答by Erunafailaro

Meanwhile, since build 149 of the jdk9, the classes

同时,由于构建了 jdk9 的 149,类

  • HttpClient
  • HttpRequest
  • HttpResponse
  • WebSocket
  • HttpClient
  • HttpRequest
  • HttpResponse
  • WebSocket

have been moved to the package jdk.incubator.http. They are part of the jigsaw module jdk.incubator.httpclient. See ticket JDK-8170648for more details.

已移至包中jdk.incubator.http。它们是拼图模块的一部分jdk.incubator.httpclient。有关更多详细信息,请参阅票证JDK-8170648

So you have to change your imports to jdk.incubator.http.*. Furthermore, you must include the module jdk.incubator.httpclientin your module-info.java. When compiling and running your code, add the argument --add-modules=jdk.incubator.httpclientto your invocation of the java and javac executables.

因此,您必须将导入更改为jdk.incubator.http.*. 此外,您必须将该模块包含jdk.incubator.httpclient在您的module-info.java. 在编译和运行您的代码时,将参数添加 --add-modules=jdk.incubator.httpclient到您对 java 和 javac 可执行文件的调用中。

All classes related to the http client have been removed from jdk9. They are included as incubator features and are no longer part of the API. Hopefully, the new client will be part of jdk10.

与 http 客户端相关的所有类已从 jdk9 中删除。它们作为孵化器功能包含在内,不再是 API 的一部分。希望新客户端将成为 jdk10 的一部分。

回答by Yuri Schimke

In JDK 11, the package is

在 JDK 11 中,包是

import java.net.http.HttpClient;

And the module name is java.net.http

模块名称是 java.net.http