"package java.net.http does not exist" error on JDK9
Asked Answered
M

5

18

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?

Misapprehension answered 28/4, 2016 at 9:36 Comment(0)
U
10

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.

Upheld answered 28/4, 2016 at 10:2 Comment(1)
This is outdated and replaced by the answer I believeMuriah
I
10

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.

Impasse answered 30/3, 2017 at 7:56 Comment(0)
H
1

In JDK 11, the package is

import java.net.http.HttpClient;

And the module name is java.net.http

Hamfurd answered 3/7, 2018 at 6:52 Comment(2)
I just was doing some experiments in JDK 10, does it make sense to use JDK 11 right now before the official release? What do you think?Galantine
Not for prod. But if you are developing a library then yes given it is a LTS version and the 6 month cycleHamfurd
R
1

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.

Rubberneck answered 8/6, 2022 at 16:18 Comment(0)
O
0

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.
Ovenware answered 17/12, 2021 at 17:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.