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

时间:2016-04-28 09:36:45

标签: java java-9

我在从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的类编译并运行没有任何问题。

知道发生了什么事吗?

3 个答案:

答案 0 :(得分:9)

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

java.httpclient

您可以在module summary中找到JDK模块列表。

答案 1 :(得分:7)

同时,自从构建149的jdk9,类

  • HttpClient
  • HttpRequest
  • HttpResponse
  • WebSocket

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

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

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

答案 2 :(得分:1)

在JDK 11中,程序包为

import java.net.http.HttpClient;

模块名称为java.net.http