Unable to export a package from java.base module

Naman picture Naman · Mar 1, 2017 · Viewed 30.3k times · Source

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 -

Directory Structure

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.

Answer

Nicolai Parlog picture Nicolai Parlog · Mar 1, 2017

The module java.base does not export the package jdk.internal.misc., so the type jdk.internal.misc.Unsafe is not accessible - as a consequence compilation fails.

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) and when running (java) the code.