Java 9:将包导出到未命名的模块失败

Ita*_*iha 13 java java-platform-module-system java-9

我正在尝试构建一个针对Java 9 的开源项目.我需要使用反射访问一些文件,但我不能,因为它们的模块不会导出包.我使用参数将包导出到未命名的模块--add-exports.

我在环境变量中添加了以下参数_JAVA_OPTIONS:

-Dsun.reflect.debugModuleAccessChecks=true 
--add-exports=javafx.graphics/com.sun.javafx.scene.traversal=ALL-UNNAMED
--add-exports=javafx.controls/com.sun.javafx.scene.control.behavior=ALL-UNNAMED
Run Code Online (Sandbox Code Playgroud)

我正在使用最新的JDK 9版本(截至今天):

C:\controlsfx>java -version
java version "9"
Java(TM) SE Runtime Environment (build 9+175)
Java HotSpot(TM) 64-Bit Server VM (build 9+175, mixed mode)
Run Code Online (Sandbox Code Playgroud)

这是我尝试构建项目时的输出:

C:\controlsfx>.\gradlew :controlsfx:build
Picked up _JAVA_OPTIONS: -Dsun.reflect.debugModuleAccessChecks=true --add-exports=javafx.graphics/com.sun.javafx.scene.traversal=ALL-UNNAMED --add-exports=javafx.controls/com.sun.javafx.scene.control.behavior=ALL-UNNAMED
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
:controlsfx:compileJava
C:\controlsfx\controlsfx\src\main\java\impl\org\controlsfx\behavior\SnapshotViewBehavior.java:60: error: package com.sun.javafx.scene.control.behavior is not visible
import com.sun.javafx.scene.control.behavior.BehaviorBase;
                                   ^
  (package com.sun.javafx.scene.control.behavior is declared in module javafx.controls, which does not export it to the unnamed module)

C:\controlsfx\src\main\java\impl\org\controlsfx\ReflectionUtils.java:3: error: package com.sun.javafx.scene.traversal is not visible
import com.sun.javafx.scene.traversal.ParentTraversalEngine;
                           ^
  (package com.sun.javafx.scene.traversal is declared in module javafx.graphics, which does not export it to the unnamed module)
Run Code Online (Sandbox Code Playgroud)

编译仍然失败,这让我想知道我做错了什么.

Nic*_*lai 8

看起来你添加的标志(似乎是正确的标志)不会添加到编译器中,而是添加到运行Gradle的进程.这是告知您的消息的指示,--illegal-access仅在java,而不是javac.

使用Java 9时,有时很难将参数传递到正确的位置.对于Gradle,这可能有所帮助.

  • 最好在此处复制给定链接的相关部分-外部网站可能会离线或随着时间的推移更改其结构 (2认同)