Java 9: Exporting packages to unnamed modules fail
Asked Answered
O

2

14

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.

Orangeman answered 26/6, 2017 at 9:55 Comment(0)
C
8

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.

Convocation answered 26/6, 2017 at 15:9 Comment(2)
it would be nice to have the relevant part of given link reproduced here - external sites can go offline or change their structure over timeDonatelli
I don't consider how to do the right think in Gradle a core part of this answer - it was added as a follow-up. The relevant information is that JAVA_OPTIONS doesn't do the trick and the arguments need to be applied in the right places. But maybe I'm wrong. If your comment gets some upvotes, I can include the details.Convocation
O
0

(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'
Omidyar answered 30/6, 2017 at 10:40 Comment(2)
You are right and @Nicolai's answer already pointed me in the right direction. Note: You need a lot more than those flags to compile the project ;)Orangeman
How to use in maven-compiler-plugin ?Assemblyman

© 2022 - 2024 — McMap. All rights reserved.