Test task and Java 9

Hi

I am using JDK build 171 and have to add extra modules when launching tests in my project.
So I added this block of configuration in my build script:

test {
jvmArgs ‘–add-modules java.xml.bind’
}

However when I run the tests I get an error: “Unrecognized option: --add-modules java.xml.bind”

However if I append this argument(–add-modules java.xml.bind) to JAVA_JDK_OPTIONS tests are run successfully.

Please, advise

Hey Sergey,

would you try to do this instead:

test {
    jvmArgs '--add-modules', 'java.xml.bind'
}

Note the separation between the option and the argument.

Cheers,
Stefan

Hi @Stefan_Wolf

Thank you for the suggestion. Yes, it worked however I got another error when compiling the project:

java.lang.IllegalAccessError: class lombok.javac.apt.LombokProcessor (in unnamed module @0x277818a4) cannot access class com.sun.tools.javac.util.Context (in module jdk.compiler) because module jdk.compiler does not export com.sun.tools.javac.util to unnamed module @0x277818a4

I tried the following JAVA_JDK_OPTIONS: --permit-illegal-access -Djdk.attach.allowAttachSelf=true --add-exports jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED

However Gradle still complains about compilation error. Can you please advise?

Hi @Strannik,

First of all, Gradle doesn’t run on Java 9 yet. I guess you’ve seen it, but to be clear, it means that you need to run Gradle using on JDK 8, then fork compilation and testing to use Java 9.

As such, I think your compileJava task should be configured to something like this:

compileJava {
    sourceCompatibility = '9'
    targetCompatibility = '9'
    options.with {
        fork = true
        forkOptions.javaHome = new File('/opt/jdk1.9.0')
        compilerArgs.addAll(['--add-exports', 'jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED'])
    }
}

The --permit-illegal-access and -Djdk.attach.allowAttachSelf=true flags are only relevant to runtime, AFAIK, not compile time.

1 Like

I find that gradle 3.1 with Groovy 2.4.7 or later runs fine on JDK9 with the necessary --add-opens.
See https://github.com/gradle/gradle/issues/1461#issuecomment-292479807 for a wrapper shell script.

JDK_JAVA_OPTIONS is used to pass options to java.
JDK_JAVAC_OPTIONS is used to pass options to javac (starting in b157).
So rather than cluttering your gradle code with JDK9 stuff, you might want to use this environment variable.
My recommendation is to hide JDK9 options required only for the gradle build in the gradle script.

Starting in Jake b172, the --add-opens are no longer required. Also --permit-illegal-access goes away and the new–illegal-access=permit is the default.
Hopefully that will migrate into JDK9 in the near future.

Hi @sdfelts

Thank you for detailed explanation. I just tested my project on the latest build b173 and it seems that --illegal-access is not supported yet(I got unrecognized flag error when starting my build) .

–permit-illegal-access flag works fine and replaces all those --add-open stuff.

Thanks.