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

2

6

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?

Coati answered 24/9, 2017 at 7:59 Comment(1)
In case you are still interested here is a Java 8 backport: github.com/stefan-zobel/http2client-java8 (I'm the maintainer of this project)Throughway
C
6

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.

Cristoforo answered 24/9, 2017 at 8:7 Comment(5)
it's not removal 100% of the time, it might be simply finalized and left into idk after all; just a small wording issue I guessMet
@Met Indeed. I quoted the javadoc linked though..it happens to be that it contradicts the thought behind the Incubator JEP.Cristoforo
since the incubating feature is mostly implemented by java source code, I tried to copy and compile it to a jar by myself, but the Java 8 compiler complained there is some internal API is missing... The missing API is like System.Logger, ...misc.Unsafe, AsnycResult etc.Coati
@IanHu Not sure of what you copied. But eventually there is a possibility that a lot of it might be depending on internal APIs which might not again be available in Java8. Also do note the point of consuming such APIs with License. ;)Cristoforo
@Met When finalized, at least the package names will change (moving from jdk.incubator to java.something), so these specific packages will be removed.Johnsiejohnson
J
3

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.

Johnsiejohnson answered 24/9, 2017 at 8:27 Comment(2)
I tried to copy the source code from jdk9 and compiled it, but it failed with some missing internal API.Coati
For the sake of completeness: Just for fun, I've attempted such a backport not so long ago. In principle, I'd say it works though I'd still consider its status as experimental.Throughway

© 2022 - 2024 — McMap. All rights reserved.