Unrecognized option: --add-opens when passed with _JAVA_OPTIONS

56.6k Views Asked by At

I am trying to fix the issue with running Nexus IQ scanner using Java 17 runtime.

The error is

java.lang.reflect.InaccessibleObjectException: Unable to make protected final java.lang.Class java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain) throws java.lang.ClassFormatError accessible: module java.base does not "opens java.lang" to unnamed module @7225790e

So I try to pass --add-opens java.base/java.lang=ALL-UNNAMED fix to the JVM which runs nexus-iq-cli.jar.

Unfortunately, I do not have a full control over java -jar ... command which gets executed on CI, otherwise I would just pass --add-opens java.base/java.lang=ALL-UNNAMED explicitly. Because of that limitation I try to use some other ways to set global JVM argument settings. One way to do it which came to my mind is using _JAVA_OPTIONS environment variable.

The thing which surprises me is that Java quits with an error:

Unrecognized option: --add-opens

When I try to pass it as follows:

# with unix shell you can set value to some variable and execute something in one line
_JAVA_OPTIONS='--add-opens java.base/java.lang=ALL-UNNAMED' java

On the other hand, Java is totally fine and acts as expected when I pass this option directly as:

java --add-opens java.base/java.lang=ALL-UNNAMED

What am I missing and what are the other possible ways to pass --add-opens java.base/java.lang=ALL-UNNAMED argument if I do not have a direct control over the java -jar command which CI executes to perform Nexus IQ scan step?

1

There are 1 best solutions below

1
On BEST ANSWER

It turned out that since Java 9 the modern proper way to achieve what I want is using the JDK_JAVA_OPTIONS environment variable.

_JAVA_OPTIONS was an undocumented feature

Here is an excerpt from JDK 9 release notes:

JDK 9 supports a new environment variable JDK_JAVA_OPTIONS to prepend options to those specified on the command line. The new environment variable has several advantages over the legacy/unsupported _JAVA_OPTIONS environment variable including the ability to include java launcher options and @file support

My final solution to Nexus IQ issue was to add the following 2 options for JVM:

--add-opens java.base/java.lang=ALL-UNNAMED
--add-opens=java.base/java.util=ALL-UNNAMED

If you will face InaccessibleObjectException on some other package, just add one more --add-opens