Java Error with Java16 : java.lang.reflect.InaccessibleObjectException - java

Existing code is working perfectly fine with JDK11 installed. Same lines of code is throwing below error in JDK16
java.lang.reflect.InaccessibleObjectException: Unable to make private static native long[] java.util.prefs.WindowsPreferences.WindowsRegOpenKey(long,byte[],int) accessible: module java.prefs does not "opens java.util.prefs" to unnamed module #3f803fae
at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:357)
at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:297)
at java.base/java.lang.reflect.Method.checkCanSetAccessible(Method.java:199)
at java.base/java.lang.reflect.Method.setAccessible(Method.java:193)
When code is migrated from Java8 to Java9, WinRegistry.java file is modified with some of the methods' input parameter(s) type from int to long

Reading/Writing from/to Windows Registry is much simpler with Advapi32Util.java
With this JNA APIs one can easily read the Registry Keys, Values and also can set Registry with new key and value.
I used following APIs to fetch Registry value of a key with path to the registry key as below
Advapi32Util.registryGetStringValue(WinReg.HKEY_LOCAL_MACHINE, registry_path, value_of)
Also, the children key(s) under specific path can be fetched from below API
Advapi32Util.registryGetKeys(HKEY root, String keyPath)

Related

Getting error in the .print() of Flink in java in eclipse

While trying to execute a basic Flink program in eclipse, I'm getting error due to .print() called by datastream_name.print() for printing my datastream.
using Java8
ERROR
Exception in thread "main" java.lang.reflect.InaccessibleObjectException: Unable to make field private final byte[] java.lang.String.value accessible: module java.base does not "opens java.lang" to unnamed module #776aec5c
at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:354)
at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:297)
at java.base/java.lang.reflect.Field.checkCanSetAccessible(Field.java:180)
at java.base/java.lang.reflect.Field.setAccessible(Field.java:174)
at org.apache.flink.api.java.ClosureCleaner.clean(ClosureCleaner.java:106)
at org.apache.flink.api.java.ClosureCleaner.clean(ClosureCleaner.java:132)
at org.apache.flink.api.java.ClosureCleaner.clean(ClosureCleaner.java:132)
at org.apache.flink.api.java.ClosureCleaner.clean(ClosureCleaner.java:69)
at org.apache.flink.streaming.api.environment.StreamExecutionEnvironment.clean(StreamExecutionEnvironment.java:2139)
at org.apache.flink.streaming.api.datastream.DataStream.clean(DataStream.java:203)
at org.apache.flink.streaming.api.datastream.DataStream.addSink(DataStream.java:1243)
at org.apache.flink.streaming.api.datastream.DataStream.writeUsingOutputFormat(DataStream.java:1144)
at org.apache.flink.streaming.api.datastream.DataStream.writeAsText(DataStream.java:1004)
at orgname.testProjectFlink.App.main(App.java:19)
What is the flink version?
Beginning from flink1.15, the support of Java 8 is now deprecated and removed (FLINK-25247). The Flink community recommends all users migrate to Java 11.

java module access issue: "Class in a module cannot access class in unnamed module because module x does not read unnamed module y"

My source code compiles on java 7 and runs on java 11.
I am trying to integrate imperva RASP as a java agent in tomcat. However, when I start the tomcat server, it is throwing following exception:
Caused by: java.lang.IllegalAccessError: class sun.security.ec.ECDSASignature (in module jdk.crypto.ec) cannot access class com.imperva.rasp.AgentBridge (in unnamed module #0x66c61024) because module jdk.crypto.ec does not read unnamed module #0x66c61024
at jdk.crypto.ec/sun.security.ec.ECDSASignature.<init>(ECDSASignature.java:118)
at jdk.crypto.ec/sun.security.ec.ECDSASignature.<init>(ECDSASignature.java:106)
at jdk.crypto.ec/sun.security.ec.ECDSASignature$SHA1.<init>(ECDSASignature.java:214)
at jdk.crypto.ec/sun.security.ec.SunEC$ProviderService.newInstance(SunEC.java:102)
at java.base/java.security.Signature.isSpi(Signature.java:331)
The way I am passing java agent is:
JAVA_OPTS="-javaagent:$IMPERVA_JAR $JAVA_OPTS"
I went through multiple posts such as this one about --add-opens argument. Based on that, I am passing JDK_JAVA_OPTIONS="$JDK_JAVA_OPTIONS --add-opens=jdk.crypto.ec/sun.security.ec=ALL-UNNAMED"
I can see following statement in the logs:
Picked up JDK_JAVA_OPTIONS: --add-opens=jdk.crypto.ec/sun.security.ec=ALL-UNNAMED
Am I missing something here or is there any syntax error in the arguments that I am passing?
There is no any other configuration is done.
Any help is appreciated. Thank you.
Managed to solve it by using --add-reads instead of --add-opens.
JDK_JAVA_OPTIONS="$JDK_JAVA_OPTIONS --add-reads jdk.crypto.ec=ALL-UNNAMED"

Error in android development, gradle build failed

So, ever since I updated my repository from my group, I got this error, however, when I looked at the commits all that changes in the built-in files are the version of gradle and jdk. I've searched and done possible solution regarding the app:processDebugMainMAnifest and I just don't know how I can resolve this problem.
Execution failed for task ':app:processDebugMainManifest'.
Unable to make field private final java.lang.String java.io.File.path accessible: module java.base does not "opens java.io" to unnamed module #645de77a

Java's add-opens doesn't seem to make a package accessible

I am trying to access JavaFX's package-private class & public static method in it (javafx.scene.control.skin.TableSkinUtils.resizeColumnToFitContent), so that I can resize TableView's columns automatically.
However, I'm using the module system since it seems that JavaFX 11 requires my app to be a module. If I don't specify a module for my app, I get this error: Error: JavaFX runtime components are missing, and are required to run this application I'd actually prefer to not use the module system at all, is there a way?
Now, due to the module system, I get an exception when attempting to make the method TableSkinUtils.resizeColumnToFitContent accessible:
Caused by: java.lang.reflect.InaccessibleObjectException: Unable to make public static void javafx.scene.control.skin.TableSkinUtils.resizeColumnToFitContent(javafx.scene.control.skin.TableViewSkinBase,javafx.scene.control.TableColumnBase,int) accessible: module javafx.controls does not "opens javafx.scene.control.skin" to module Explorer
So, I tried to add this workaround: --add-opens javafx.controls/javafx.scene.control.skin=ALL-UNNAMED to Intellij's run configuration VM options.
However, with or without the above, I get the same exception.
What am I possibly doing wrong?

How to get a Java 9 ModuleReference for an unnamed module [duplicate]

This question already has answers here:
Scanning classpath/modulepath in runtime in Java 9
(2 answers)
Closed 4 years ago.
Update: I answered my own question here:
Scanning classpath/modulepath in runtime in Java 9
--
[Old question -- obsolete:]
What is the correct way to get a ModuleReference given only a Module object in Java 9?
Consider these two methods of referring to java.base:
Module mod = ModuleLayer.boot().findModule("java.base").orElse(null);
ModuleReference modRef = ModuleFinder.ofSystem().find("java.base").orElse(null);
mod has a method Set<String> getPackages(), but you only get the names of the packages, you cannot list the resources in each package.
modRef has a method ModuleReader open(), and ModuleReader has a method Stream<String> list() that lists the resources in the module, which is what I need to do.
However, for automatic (and therefore unnamed) modules, produced by adding non-module jarfiles to the classpath, you cannot get a ModuleReference from ModuleFinder.ofSystem().find(String name) or ModuleFinder.ofSystem().findAll() -- you can only get the Module reference from getClass().getModule().
I cannot find any way to get a ModuleReference for automatic modules. I also cannot find a way to get a ModuleReference from a Module object, which means I am not able to list the resources in a Module if the module is automatic and/or unnamed.
Surely there must be a way to get a ModuleReference for a given (already-loaded) Module?
First thing to clear out from the question is that the inference is incorrect in the sentence -
for automatic (and therefore unnamed) modules, produced by adding non-module jar files to the classpath
Automatic modules are named modules found on the module path. On the other hand, the unnamed module is a member of the module system which supports loading a type from the classpath whose package is not defined in any known(named) module.
The ClassLoader that was used to load the type itself can be used to get the Module(unnamed). I've tried explaining this in the answer to How many unnamed modules are created in Java 9? as well. To link to the precise documentation over the same that also answers with which class loader is the unnamed module associated?
Every class loader, it turns out, has its own unique unnamed module,
which is returned by the new ClassLoader::getUnnamedModule method....
ClassLoader cl = getClass().getClassLoader();// returns the class loader for the class
Module yourClassLoaderUnnamedModule = cl.getUnnamedModule();
Moving further the document adds to getting the module of a type loaded form unnamed module::
.. type is considered to be in that loader’s unnamed module, i.e.,
the getModule method of the type’s Class object will return its
loader’s unnamed module.
which eventually is equivalent to :
Module yourClassUnnamedModule = getClass().getModule(); // from the type itself
Primarily in terms of accessing resources of the unnamed module, though I agree there is no API currently visible to access the unnamed module's resources etc. Yet, since the type of the Class objects loaded via this module would always be from the classpath, one can either get all the resources of the classpath that is in use or the other way around, you can check if the resource in use is accessed from classpath or the module path. One way I could think of is to verify using the above two functionalities as :
yourClassLoaderUnnamedModule.equals(yourClassUnnamedModule)//true if resource is loaded via classpath
The module finder returned by ModuleFinder.ofSystem() is one that locates system modules, which are the modules built into the JRE environment running the application.
If the location of the automatic module on the file system is known, you could try retrieving a module finder using the directory, i.e. using ModuleFinder.of(path). Another way is to use the module layer’s configuration to resolve the module reference:
Optional<ResolvedModule> resolvedModule = ModuleLayer.boot().configuration().findModule(name);
Optional<ModuleReference> moduleReference = resolvedModule.map(ResolvedModule::reference);
To answer your general question about obtaining a module reference from only a Module, one way to do it:
Optional<ModuleReference> moduleReference
= module.getLayer().configuration()
.findModule(module.getName())
.map(ResolvedModule::reference);
For an unnamed module, my guess is that you cannot retrieve a ModuleReference for it, but at least one can try calling configuration().modules() and see if they include an unnamed module.

Categories

Resources