简体   繁体   中英

How can I use Http Client API(since java 9) in Java8 project

Java 9 imports a new HTTP/2 Client API which seems good to use but is there any way to use it in Java 8?

OR

Is there any shim / polyfill (from javascript) available to make it available in Java 8?

Is there any way to use it in java 8?

No , because the jdk.incubator.http module has been added since Java 9 .

So it wouldn't be possible to compile it with a --release 8 option on the compiler work with Java8. You would end up getting errors as:

 $ javac --release 8 .../src/com/HttpGet.java $ .../src/com/HttpGet.java:3: error: package jdk.incubator.http does not exist import jdk.incubator.http.HttpClient; ^ 

With minimal code to reproduce this as:-

import jdk.incubator.http.HttpClient;

public class HttpGet {
    public static void main(String[] args) {
        HttpClient httpClient = HttpClient.newBuilder().followRedirects(HttpClient.Redirect.ALWAYS).build();
        System.out.println(httpClient.version());
    }
}

Moreover, the documentation clearly reads this upfront

Incubating Feature . Will be removed in a future release.

In principle, the source for it is available. You could copy it, compile it and create a jar usable with Java 8 (possibly with some changes or missing features if the code needs Java 9 anywhere), similarly to ThreeTen-Backport providing java.time for Java 6/7 .

But there doesn't seem to be one available yet (after a quick search). If you decide to go in this direction, make sure to follow the relevant licenses.

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