How Java 9 Compiled with Java 8 Works, Which Uses a Non-Exportable Package

I compiled the following code using the Java-8 compiler:

package pack;
import sun.util.calendar.CalendarUtils;
public class A {
    public static void main(String[] args) {
        System.out.println(CalendarUtils.isGregorianLeapYear(2018));
    }
}

I compiled the above code using the Java-8 compiler as:

gyan@gyan-pc:~/codes/java$ ~/Documents/softwares/Linux/jdk1.8.0_131/bin/javac -d . a.java
a.java:2: warning: CalendarUtils is internal proprietary API and may be removed in a future release
import sun.util.calendar.CalendarUtils;
                        ^
a.java:9: warning: CalendarUtils is internal proprietary API and may be removed in a future release
        System.out.println(CalendarUtils.isGregorianLeapYear(2018));
                           ^
2 warnings

The default version of my Java interpreter is:

gyan@gyan-pc:~/codes/java$ java -version
java version "9.0.1"
Java(TM) SE Runtime Environment (build 9.0.1+11)
Java HotSpot(TM) 64-Bit Server VM (build 9.0.1+11, mixed mode)

And I can run the compiled code using the Java-9 interpreter without errors.

gyan@gyan-pc:~/codes/java$ java pack.a
false

According to my information: At runtime, the package "package" will be contained inside a special module called "Dimensionless module". The "moduleless module" requires all modules of the Java platform module. But only this package can be used by the "Nameless Module", which is exported by the corresponding module.

: java.base "sun.util.calendar". , " " ?

+4
1

, Relaxed strong encapsulation : -

--illegal-access=permit , class, JDK 8. , - , API .

, , . , . .

JDK 9. , , .

,

.../jdk-9.0.1.jdk/Contents/Home/bin/java --illegal-access=deny pack.Some

, , :

Exception in thread "main" java.lang.IllegalAccessError: class
pack.Some (in unnamed module @0x1055e4af) cannot access class
sun.util.calendar.CalendarUtils (in module java.base) because module
java.base does not export sun.util.calendar to unnamed module
@0x1055e4af    at pack.Some.main(Some.java:7)
+5

Source: https://habr.com/ru/post/1692630/


All Articles