Java 9: Exporting packages to unnamed modules fail

♀尐吖头ヾ 提交于 2021-02-04 14:41:40

问题


I am trying to build an open-source project against Java 9. There are some files which I need to access using reflection but I cannot because the packages are not exported by their modules. I export the packages to unnamed modules by using the arguments --add-exports.

I have added the following arguments to environment variable _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

I am using the latest JDK 9 build (as of today):

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)

Here is the output when I try to build the project:

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)

The compilation still fails, which makes me wonder what am I doing wrong.


回答1:


It looks like the flags you add (which seem to be the right ones) are not added to the compiler but the process that runs Gradle. An indication of that are the messages that inform you about --illegal-access, which is only available on java, not javac.

When working with Java 9 it can sometimes be tough to get the arguments to the right places. For Gradle this might help.




回答2:


(Note: I could not get the project to compile, but some of the errors in question (e.g. BehaviorBase) were cleaned up with the following.)

Using jdk 9 b175 and Gradle 4.1-milestone-1:

compileJava {
    options.encoding = "UTF-8"
    options.incremental = true

    options.compilerArgs.addAll([
        "--add-exports",
        "javafx.base/com.sun.javafx.event=ALL-UNNAMED"
    ])  
    options.compilerArgs.addAll([
        "--add-exports",
        "javafx.controls/com.sun.javafx.scene.control.behavior=ALL-UNNAMED"
    ])  
    options.compilerArgs.addAll([
        "--add-exports",
        "javafx.graphics/com.sun.javafx.scene.traversal=ALL-UNNAMED"
    ])  
}

and

sourceCompatibility = '9'
targetCompatibility = '9'


来源:https://stackoverflow.com/questions/44757102/java-9-exporting-packages-to-unnamed-modules-fail

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!