How do I properly add --add-opens to jvmArgs in JavaExec task

arguments, gradle, java, reflection

Solution

This question was answered in a comment by `Jorn Vernee` (no relation to me despite the similar name), so I will duplicate it here:

You probably need to pass both as separate strings, not one `('--add-opens', 'java.base/jdk.internal.loader=ALL-UNNAMED')`. I think it should also work if you replace the space in the argument string with an `=`.

Problem

Currently I have a task that executes to run the program, something like ``` task runApp(type: JavaExec) { classpath = sourceSets.main.runtimeClasspath main = 'com.eonmux.somepackage' jvmArgs '-Xmx4096m' } ``` I'm trying to add --add-opens to the list of JVM args so that I can allow reflection access from my gradle packages Something like ``` task runApp(type: JavaExec) { classpath = sourceSets.main.runtimeClasspath main = 'com.eonmux.somepackage' jvmArgs '-Xmx4096m','--add-opens java.base/jdk.internal.loader=ALL-UNNAMED' } ``` However this gives the following error saying that the command line option is unrecognized ``` 2020-12-09T16:30:56.285-0500 [INFO] [org.gradle.process.internal.DefaultExecHandle] Successfully started process 'command 'C:\Program Files\AdoptOpenJDK\jdk-11.0.6.10-openj9\bin\java.exe'' 2020-12-09T16:30:56.286-0500 [DEBUG] [org.gradle.process.internal.ExecHandleRunner] waiting until streams are handled... 2020-12-09T16:30:56.360-0500 [ERROR] [system.err] JVMJ9VM007E Command-line option unrecognised: --add-opens java.base/jdk.internal.loader=ALL-UNNAMED 2020-12-09T16:30:56.361-0500 [ERROR] [system.err] Error: Could not create the Java Virtual Machine. Could not create the Java Virtual Machine. 2020-12-09T16:30:56.362-0500 [ERROR] [system.err] Error: A fatal exception has occurred. Program will exit. A fatal exception has occurred. Program will exit. ``` Which is strange because when I open up a command line to the Java path indicated, and run C:\Program Files\AdoptOpenJDK\jdk-11.0.6.10-openj9\bin>java --add-opens I get the following message confirming its existence ``` Error: --add-opens requires modules to be specified ``` I'm no Gradle configuration expert, so I'd appreciate some help. This is on Windows 10x64 with the IntelliJ IDE and Gradle 6.0.1

Original source