Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
277 views
in Technique[技术] by (71.8m points)

"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.module1srcorg.example.module1orgexampleHttp2.java:6: error: package java.net.http does not exist
import java.net.http.HttpRequest;
                    ^
org.example.module1srcorg.example.module1orgexampleHttp2.java:7: error: package java.net.http does not exist
import java.net.http.HttpResponse;
                    ^
org.example.module1srcorg.example.module1orgexampleHttp2.java:9: error: package java.net.http does not exist
import static java.net.http.HttpRequest.noBody;
                           ^
org.example.module1srcorg.example.module1orgexampleHttp2.java:9: error: static import only from classes and interfaces
import static java.net.http.HttpRequest.noBody;
^
org.example.module1srcorg.example.module1orgexampleHttp2.java:10: error: package java.net.http does not exist
import static java.net.http.HttpResponse.asString;
                           ^
org.example.module1srcorg.example.module1orgexampleHttp2.java:10: error: static import only from classes and interfaces
import static java.net.http.HttpResponse.asString;
^
org.example.module1srcorg.example.module1orgexampleHttp2.java:14: error: cannot find symbol
        HttpResponse response = HttpRequest
        ^
  symbol:   class HttpResponse
  location: class Http2
org.example.module1srcorg.example.module1orgexampleHttp2.java:14: error: cannot find symbol
        HttpResponse response = HttpRequest
                                ^
  symbol:   variable HttpRequest
  location: class Http2
org.example.module1srcorg.example.module1orgexampleHttp2.java:16: error: cannot find symbol
                .body(noBody())
                      ^
  symbol:   method noBody()
  location: class Http2
org.example.module1srcorg.example.module1orgexampleHttp2.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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...