Search code examples
javamavenjava-11java-modulejava-http-client

Use jdk.internal.net.http


I would like to use ResponseSubscribers.ByteArraySubscriber that is in jdk.internal.net.http. I use openjdk11.

I tried 2 things :

1/ I added to maven compiler plugin exports module

<compilerArgs>
    <arg>--add-exports</arg><arg>java.net.http/jdk.internal.net.http=fr.app</arg>
</compilerArgs>

-> it compile !

2/ I created module-info.java

module fr.app {
    requires java.net.http;

    requires com.fasterxml.jackson.core;
    requires com.fasterxml.jackson.databind;
    requires com.fasterxml.jackson.datatype.jsr310;

    exports fr.app;
    exports fr.app.parser;
}

There is an error when running a junit test that use the class importing jdk.internal.net.http

fr.app.AppException: java.io.IOException: class fr.app.MyClass$BodySubscribers (in unnamed module @0x6537cf78) cannot access class jdk.internal.net.http.ResponseSubscribers$ByteArraySubscriber (in module java.net.http) because module java.net.http does not export jdk.internal.net.http to unnamed module @0x6537cf78

I understand BodySubscribers must be exported only in named module. But my module is name fr.app right ?


Solution

  • But my module is name fr.app right ?

    Not really, while you've created the module-info.java in your project, during the execution of your application your actual code seems to be found on the classpath eventually.

    Hence your MyClass is residing in the unnamed module and the error reads as follows

    class fr.app.MyClass$BodySubscribers (in unnamed module.....


    On another note, the class you've mentioned seems to be packaged internal to the java.net.http module and should not be relied upon from your code. You must implement your own subscriber even if you desire a similar functionality as the code you're looking at. Since the module wouldn't be exporting it for a public use anyway.