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

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);

}

}

I'm getting package java.net.http does not exist error when compiling using 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.

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

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:

module org.example {

requires java.httpclient;

}

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值