我是靠谱客的博主 俊秀小蚂蚁,最近开发中收集的这篇文章主要介绍java base包,无法从java.base模块导出包,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

Using IDEA-EAP for JDK9 development experiments.

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)

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.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 -

9xvky.png

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.

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's answer is correct regarding the techniques necessary to export an otherwise unexported package from the java.base module or from any other module.

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;

to your module-info.java file.

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

Even though Unsafe is in the jdk.unsupported module, this technique is supported in JDK 9, in accordance with JEP 260.

最后

以上就是俊秀小蚂蚁为你收集整理的java base包,无法从java.base模块导出包的全部内容,希望文章能够帮你解决java base包,无法从java.base模块导出包所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(36)

评论列表共有 0 条评论

立即
投稿
返回
顶部