无法从 java.base 模块导出包

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42538750/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 00:12:53  来源:igfitidea点击:

Unable to export a package from java.base module

javajava-9jigsawjava-modulemodule-info

提问by Naman

Using IDEA-EAP for JDK9 development experiments.

使用 IDEA-EAP 进行 JDK9 开发实验。

I am getting the following error -

我收到以下错误 -

Error:(3, 20) java: package jdk.internal.misc is not visible  
(package jdk.internal.misc is declared in module java.base, which does
not export it to module com.jigsaw.npe)
Error:(3, 20) java: package jdk.internal.misc is not visible  
(package jdk.internal.misc is declared in module java.base, which does
not export it to module com.jigsaw.npe)

The class definition is as -

类定义为 -

package experiment;
import jdk.internal.misc.Unsafe;

public class CompareAndSwap {

    static Unsafe UNSAFE = Unsafe.getUnsafe();
    ...
}

I've tried including a module-info.javafile as well inside the module created using the IDE with the following statements -

我已经尝试module-info.java在使用 IDE 创建的模块中包含一个文件以及以下语句 -

module com.jigsaw.npe {
    requires java.base;
}

Directory structure now looks as depicted in the picture -

目录结构现在看起来如图所示 -

Directory Structure

目录结构

The IDE though reflects the module-info.javaas unused and probably this is the reason that I am not able to define the module com.jigsaw.npeas tried above.

虽然 IDE 反映了module-info.java未使用,这可能是我无法module com.jigsaw.npe按照上面尝试定义的原因。

Looking for some help on to how to correctly place the module-info.java and/or anything other than that which I've missed.

寻找有关如何正确放置 module-info.java 和/或我错过的任何其他内容的帮助。

采纳答案by Stuart Marks

Nicolai's answeris correct regarding the techniques necessary to export an otherwise unexported package from the java.basemodule or from any other module.

对于从java.base模块或任何其他模块导出未导出的包所需的技术,Nicolai 的回答是正确的。

But if the goal is to use Unsafe, the way to do so is to use sun.misc.Unsafewhich is exported by the jdk.unsupportedmodule. If you're compiling your code for the unnamed module, you needn't do anything special regarding modules to get access to it. If you're compiling code in a module, you need to add

但是如果目标是使用Unsafe,那么这样做的方法是使用模块sun.misc.Unsafe导出的 which jdk.unsupported。如果您正在为未命名模块编译代码,则无需对模块执行任何特殊操作即可访问它。如果要在模块中编译代码,则需要添加

requires jdk.unsupported;

to your module-info.javafile.

到您的module-info.java文件。

To use Unsafeyou have to do the reflective setAccessibletechnique to get access to the field, which is the same as you had to do in previous JDK releases:

要使用,Unsafe您必须使用反射setAccessible技术来访问该字段,这与您在以前的 JDK 版本中必须执行的操作相同:

import sun.misc.Unsafe;

...

Field theUnsafeField = Unsafe.class.getDeclaredField("theUnsafe");
theUnsafeField.setAccessible(true);
Unsafe theUnsafe = (Unsafe)theUnsafeField.get(null);

Even though Unsafeis in the jdk.unsupportedmodule, this technique is supported in JDK 9, in accordance with JEP 260.

即使Unsafejdk.unsupported模块中,根据JEP 260,JDK 9 也支持此技术。

回答by Nicolai

The module java.basedoes not exportthe package jdk.internal.misc., so the type jdk.internal.misc.Unsafeis not accessible- as a consequence compilation fails.

模块java.base导出jdk.internal.misc.,因此类型jdk.internal.misc.Unsafe不可访问- 结果编译失败。

You can make it export the package by adding the following command line option:

您可以通过添加以下命令行选项使其导出包:

# if you want to access it from com.jigsaw.npe only:
--add-exports java.base/jdk.internal.misc=com.jigsaw.npe
# if you want to access from all code:
--add-exports java.base/jdk.internal.misc=ALL-UNNAMED

You will have to do that when compiling (javac) andwhen running (java) the code.

在编译 ( javac)运行 ( java) 代码时,您必须这样做。