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?

Krzysztof Krasoń
  • 26,515
  • 16
  • 89
  • 115
Ian Hu
  • 295
  • 3
  • 7
  • 1
    In case you are still interested here is a Java 8 backport: https://github.com/stefan-zobel/http2client-java8 (I'm the maintainer of this project) – Stefan Zobel May 25 '18 at 13:52

2 Answers2

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.

Naman
  • 27,789
  • 26
  • 218
  • 353
  • 3
    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 guess – Eugene Sep 24 '17 at 09:04
  • @Eugene Indeed. I quoted the javadoc linked though..it happens to be that it contradicts the thought behind the Incubator JEP. – Naman Sep 24 '17 at 09:06
  • 1
    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. – Ian Hu Sep 24 '17 at 11:52
  • 1
    @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. ;) – Naman Sep 24 '17 at 12:29
  • 1
    @Eugene When finalized, at least the package names will change (moving from `jdk.incubator` to `java.something`), so these specific packages _will_ be removed. – Alexey Romanov Sep 24 '17 at 13:41
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.

Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487
  • I tried to copy the source code from jdk9 and compiled it, but it failed with some missing internal API. – Ian Hu Sep 24 '17 at 11:46
  • 2
    For the sake of completeness: Just for fun, I've attempted such a [backport](https://github.com/stefan-zobel/http2client-java8) not so long ago. In principle, I'd say it works though I'd still consider its status as experimental. – Stefan Zobel May 25 '18 at 13:59