简体   繁体   English

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

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

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 exist error when compiling using JDK 9:使用 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

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?知道发生了什么吗?

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:在您的模块定义中,位于(基于您的包名称)在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 模块列表。

Meanwhile, since build 149 of the jdk9, the classes同时,由于 jdk9 的 build 149,这些类

  • HttpClient
  • HttpRequest
  • HttpResponse
  • WebSocket

have been moved to the package jdk.incubator.http .已移至包jdk.incubator.http They are part of the jigsaw module jdk.incubator.httpclient .它们是拼图模块jdk.incubator.httpclient的一部分。 See ticket JDK-8170648 for more details.有关更多详细信息,请参阅票证JDK-8170648

So you have to change your imports to jdk.incubator.http.* .因此,您必须将导入更改为jdk.incubator.http.* Furthermore, you must include the module jdk.incubator.httpclient in your module-info.java .此外,您必须在module-info.java jdk.incubator.httpclient When compiling and running your code, add the argument --add-modules=jdk.incubator.httpclient to your invocation of the java and javac executables.编译和运行代码时,将参数--add-modules=jdk.incubator.httpclient添加到 java 和 javac 可执行文件的调用中。

All classes related to the http client have been removed from jdk9.所有与 http 客户端相关的类都已从 jdk9 中删除。 They are included as incubator features and are no longer part of the API.它们作为孵化器功能包含在内,不再是 API 的一部分。 Hopefully, the new client will be part of jdk10.希望新客户端将成为 jdk10 的一部分。

In JDK 11, the package is在 JDK 11 中,包是

import java.net.http.HttpClient;

And the module name is java.net.http模块名称是java.net.http

Gradle

If you are using Gradle and IntelliJ it may be because the Gradle JVM setting is using a version below Java 11.如果您使用的是 Gradle 和 IntelliJ,可能是因为Gradle JVM设置使用的版本低于 Java 11。

To change it in IntelliJ:要在 IntelliJ 中更改它:

  1. Go to File > Settings > Build, Execution, Deployment > Build Tools > Gradle转到File > Settings > Build, Execution, Deployment > Build Tools > Gradle
  2. Go to Gradle JVM setting on the right downside.转到右侧的 Gradle JVM 设置。
  3. Change it to any of Project SDK or the Java 11 version.将其更改为任何Project SDK或 Java 11 版本。

Maven

If you are using Maven and IntelliJ it may be because the JRE setting in Maven is using a version below Java 11.如果您使用的是 Maven 和 IntelliJ,可能是因为 Maven 中的JRE设置使用的是低于 Java 11 的版本。

To change it in IntelliJ:要在 IntelliJ 中更改它:

  1. Go to File > Settings > Build, Execution, Deployment > Build Tools > Maven > Runner转到File > Settings > Build, Execution, Deployment > Build Tools > Maven > Runner
  2. Go to the JRE setting on the right.转到右侧的 JRE 设置。
  3. Change it to any of Project SDK or the Java 11 version.将其更改为任何Project SDK或 Java 11 版本。

I had the exact same issue and this is how I resolved it.我遇到了完全相同的问题,这就是我解决它的方法。 In the gradle, I already had在毕业典礼上,我已经有了

tasks.withType<KotlinCompile>() {
    kotlinOptions.jvmTarget = "11"
}

I added Gradle JVM as 11. This is in Build, Exectution, Deployment --> Build Tools --> Gradle Additionally, I added this snippet in the build.gradle我将 Gradle JVM 添加为 11。这是在Build, Exectution, Deployment --> Build Tools --> Gradle另外,我在 build.gradle 添加了这个片段

java {
    sourceCompatibility = JavaVersion.VERSION_11
    targetCompatibility = JavaVersion.VERSION_11
}

Plus I also made my module SDK as 11.另外,我还将我的模块 SDK 设为 11。

Let me know if this does not work for you.如果这对您不起作用,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM