简体   繁体   中英

"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:

{ 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 .

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.

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.

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

Gradle

If you are using Gradle and IntelliJ it may be because the Gradle JVM setting is using a version below Java 11.

To change it in IntelliJ:

  1. Go to File > Settings > Build, Execution, Deployment > Build Tools > Gradle
  2. Go to Gradle JVM setting on the right downside.
  3. Change it to any of Project SDK or the Java 11 version.

Maven

If you are using Maven and IntelliJ it may be because the JRE setting in Maven is using a version below Java 11.

To change it in IntelliJ:

  1. Go to File > Settings > Build, Execution, Deployment > Build Tools > Maven > Runner
  2. Go to the JRE setting on the right.
  3. Change it to any of Project SDK or the Java 11 version.

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

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

Plus I also made my module SDK as 11.

Let me know if this does not work for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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