“package java.net.http不存在” JDK9上的错误 [英] "package java.net.http does not exist" error on JDK9
问题描述
我在编写HttpRequest JavaDoc中的简单阻塞GET示例时遇到问题:
I have a problem compiling simple blocking GET example from the HttpRequest JavaDoc:
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);
}
}
我收到包使用JDK 9编译时,java.net.http不存在
错误:
{ 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发生相同的错误。
Same error occurs using command line and IntelliJ.
我的模块没有问题,因为没有java.net.http的类编译并运行没有任何问题。
It is not a problem with my module because classes without java.net.http compiles and run without any problem.
知道发生了什么事吗?
推荐答案
在模块定义中,在 src / org / example / module-info.java
中找到(基于您的包名),您需要将依赖项添加到 java。 net.http
包,它包含在 java.httpclient
模块中:
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.http
package, which is included in the java.httpclient
module:
module org.example {
requires java.httpclient;
}
您可以在模块摘要。
这篇关于“package java.net.http不存在” JDK9上的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!