有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java使用jdk。内部的网http

我想使用ResponseSubscribers.ByteArraySubscriber中的jdk.internal.net.http。我使用openjdk11

我尝试了两件事:

一,/ 我添加了maven编译器插件导出模块

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

->;是的

二,/ 我创建了模块信息。爪哇

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;
}

运行使用类导入jdk.internal.net.http的junit测试时出错

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

我理解BodySubscribers必须仅在命名模块中导出。但我的模块名为fr.app,对吗


共 (2) 个答案

  1. # 1 楼答案

    But my module is name fr.app right ?

    不完全是这样,当您在项目中创建了module-info.java时,在应用程序的执行过程中,您的实际代码似乎最终会在类路径上找到

    因此,您的MyClass位于the unnamed module中,错误如下

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


    另一方面,您提到的类似乎是打包到java.net.http模块的内部,不应该依赖于您的代码。您必须实现自己的订阅服务器,即使您希望获得与正在查看的代码类似的功能。因为模块无论如何都不会导出供公共使用

  2. # 2 楼答案

    您不应该直接使用任何jdk.internal.*包中的任何类。您是否尝试过使用公共API HttpResponse.BodySubscribers.ofByteArray()