[英]"package java.net.http does not exist" error on JDK9
我在从 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);
}
}
使用 JDK 9 编译时出现package java.net.http does not exist
错误:
{ 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 的类编译和运行没有任何问题。
知道发生了什么吗?
在您的模块定义中,位于(基于您的包名称)在src/org/example/module-info.java
中,您需要将依赖项添加到java.net.http
包中,该包包含在java.httpclient
模块中:
module org.example {
requires java.httpclient;
}
您可以在模块摘要中找到 JDK 模块列表。
同时,由于 jdk9 的 build 149,这些类
HttpClient
HttpRequest
HttpResponse
WebSocket
已移至包jdk.incubator.http
。 它们是拼图模块jdk.incubator.httpclient
的一部分。 有关更多详细信息,请参阅票证JDK-8170648 。
因此,您必须将导入更改为jdk.incubator.http.*
。 此外,您必须在module-info.java
jdk.incubator.httpclient
编译和运行代码时,将参数--add-modules=jdk.incubator.httpclient
添加到 java 和 javac 可执行文件的调用中。
所有与 http 客户端相关的类都已从 jdk9 中删除。 它们作为孵化器功能包含在内,不再是 API 的一部分。 希望新客户端将成为 jdk10 的一部分。
在 JDK 11 中,包是
import java.net.http.HttpClient;
模块名称是java.net.http
Gradle
如果您使用的是 Gradle 和 IntelliJ,可能是因为Gradle JVM
设置使用的版本低于 Java 11。
要在 IntelliJ 中更改它:
File
> Settings
> Build, Execution, Deployment
> Build Tools
> Gradle
Project SDK
或 Java 11 版本。 Maven
如果您使用的是 Maven 和 IntelliJ,可能是因为 Maven 中的JRE
设置使用的是低于 Java 11 的版本。
要在 IntelliJ 中更改它:
File
> Settings
> Build, Execution, Deployment
> Build Tools
> Maven
> Runner
Project SDK
或 Java 11 版本。我遇到了完全相同的问题,这就是我解决它的方法。 在毕业典礼上,我已经有了
tasks.withType<KotlinCompile>() {
kotlinOptions.jvmTarget = "11"
}
我将 Gradle JVM 添加为 11。这是在Build, Exectution, Deployment --> Build Tools --> Gradle
另外,我在 build.gradle 添加了这个片段
java {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}
另外,我还将我的模块 SDK 设为 11。
如果这对您不起作用,请告诉我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.