“package java.net.http does not exist” error on JDK9
“package java.net.http does not exist” error on 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:
package java.net.http does not exist
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?
3 Answers
3
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.
Meanwhile, since build 149 of the jdk9, the classes
HttpClient
HttpRequest
HttpResponse
WebSocket
have been moved to the package jdk.incubator.http
. They are part of the jigsaw module jdk.incubator.httpclient
. See ticket JDK-8170648 for more details.
jdk.incubator.http
jdk.incubator.httpclient
So you have to change your imports to jdk.incubator.http.*
. Furthermore, you must include the module jdk.incubator.httpclient
in your module-info.java
. When compiling and running your code, add the argument --add-modules=jdk.incubator.httpclient
to your invocation of the java and javac executables.
jdk.incubator.http.*
jdk.incubator.httpclient
module-info.java
--add-modules=jdk.incubator.httpclient
All classes related to the http client have been removed from jdk9. They are included as incubator features and are no longer part of the API. Hopefully, the new client will be part of jdk10.
In JDK 11, the package is
import java.net.http.HttpClient;
And the module name is java.net.http
java.net.http
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
This is outdated and replaced by the answer I believe
– nullpointer
Nov 23 '17 at 5:01