无法从java.base模块导出包 [英] Unable to export a package from java.base module

查看:2510
本文介绍了无法从java.base模块导出包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Using IDEA-EAP for JDK9 development experiments.

我收到以下错误-

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)

类定义为-

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

public class CompareAndSwap {

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

我已经尝试在包含IDE的模块中添加module-info.java文件,并使用以下语句-

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

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

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

Directory structure now looks as depicted in the picture -

尽管IDE将module-info.java反映为未使用,但这可能是我无法按上述方法定义module com.jigsaw.npe的原因.

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

正在寻找有关如何正确放置module-info.java和/或除我遗漏之外的任何内容的帮助.

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

推荐答案

Nicolai的答案关于从java.base模块或任何其他模块中导出否则未导出的程序包.

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

但是,如果目标是使用Unsafe,则使用sun.misc.Unsafe的方法是通过jdk.unsupported模块导出.如果您要为未命名的模块编译代码,则无需对模块进行任何特殊处理即可访问它.如果要在模块中编译代码,则需要添加

But if the goal is to use Unsafe, the way to do so is to use sun.misc.Unsafe which is exported by the jdk.unsupported module. 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

requires jdk.unsupported;

到您的module-info.java文件.

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

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

import sun.misc.Unsafe;

...

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

即使Unsafejdk.unsupported模块中,根据 查看全文

相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆