Unrecognized option: --add-opens when passed with _JAVA_OPTIONS
Asked Answered
D

2

7

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?

Dagnah answered 13/12, 2022 at 10:55 Comment(0)
D
14

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

Dagnah answered 13/12, 2022 at 11:1 Comment(1)
It has an issue with some operating system: see github.com/adoptium/adoptium-support/issues/546 and github.com/adoptium/adoptium-support/issues/303 where it is suggested to use JAVA_TOOL_OPTIONS as a temporary workaround.Dawson
A
0

For Azure Spring App I have added below and it worked.

Environment Variable :

JDK_JAVA_OPTIONS=--add-opens java.base/java.util=ALL-UNNAMED --add-opens=java.base/java.net=ALL-UNNAMED
Amish answered 30/4 at 15:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.