编程语言
首页 > 编程语言> > 如何使用Java 8运行使用非导出包的Java 9编译代码

如何使用Java 8运行使用非导出包的Java 9编译代码

作者:互联网

我使用Java-8编译器编译了以下代码:

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

我使用Java-8编译器编译了上面的代码:

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

我的默认Java解释器的版本:

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)

我可以使用Java-9解释器运行已编译的代码而不会出现任何错误.

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

据我所知:在运行时,包“pack”将包含在一个名为“Unnamed module”的特殊模块中. “未命名模块”需要Java平台模块中的所有模块.但是只有那个包可以由相应模块导出的“未命名模块”使用.

我的问题是:
这里的模块java.base没有导出包“sun.util.calendar”.然后“未命名的模块”如何使用它?

解决方法:

截至pointed by Alan,第Relaxed strong encapsulation条在这方面指出如下: –

--illegal-access=permit opens each package in each module in the run-time image to code in all unnamed modules, i.e., to code on the
class path, if that package existed in JDK 8. This enables both static
access, i.e., by compiled bytecode, and deep reflective access, via
the platform’s various reflection APIs.

The first reflective-access operation to any such package causes a
warning to be issued, but no warnings are issued after that point.
This single warning describes how to enable further warnings. This
warning cannot be suppressed.

This mode is the default in JDK 9. It will be phased out in a future
release and, eventually, removed.

此外,如果您尝试使用执行编译的代码

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

使用future default标志,您将无法使用以下跟踪按预期执行代码:

06001

标签:java,java-9,java-module
来源: https://codeday.me/bug/20190622/1263181.html