error tried to access method sun.security.rsa.RSAPublicKeyImpl - java

Hi I try to execute my Spring application with dependency of JWT package and I found this error:
Caused by: java.lang.IllegalAccessError: tried to access method sun.security.rsa.RSAPublicKeyImpl.<init>([B)V from class com.agospass.OAuth2ResourceServerConfig
at com.agospass.OAuth2ResourceServerConfig.accessTokenConverter(OAuth2ResourceServerConfig.java:87)
at com.agospass.OAuth2ResourceServerConfig$$EnhancerBySpringCGLIB$$e9e14628.CGLIB$accessTokenConverter$3(<generated>)
at com.agospass.OAuth2ResourceServerConfig$$EnhancerBySpringCGLIB$$e9e14628$$FastClassBySpringCGLIB$$e622f9ed.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:358)
at com.agospass.OAuth2ResourceServerConfig$$EnhancerBySpringCGLIB$$e9e14628.accessTokenConverter(<generated>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162)
... 50 common frames omitted
My jdk version 8u241 and Maven 3.8.1
someone who can help me solve this problem or replace the dependency?

public class IllegalAccessError extends IncompatibleClassChangeError
Thrown if an application attempts to access or modify a field, or to
call a method that it does not have access to. Normally, this error is
caught by the compiler; this error can only occur at run time if the
definition of a class has incompatibly changed.
https://docs.oracle.com/javase/8/docs/api/java/lang/IllegalAccessError.html
Edited: For JDK 11
https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/IllegalAccessError.html
Certainly, your class OAuth2ResourceServerConfig's method accessTokenConverter is trying to invoke the method from a different class available in different jar than one it compile with. This behaviour is often seen when the same class (with same package) is present in different jars with difference in the accessibility of fields/methods.
To put it simply, one can say that at compile time your code has access to public method of class available in jar one but at runtime, thanks to classloader, your code is trying to access the private method of same class available in jar two.
You can look for RSAPublicKeyImpl class inside the dependencies present on the classpath and update/adjust the dependencies accordingly to get rid of the issue.

Related

"LinkageError: attempted duplicate class definition" when dynamically instrumenting java classes with ASM

I wrote a javaagent by myself to dynamically instrumenting the java classes with ASM (I am not using the COMPUTE_MAXS or COMPUTE_FRAMES of ASM, I do that manually by myself). Actually, I am just trying to use a big try-catch block for non-constructor methods to capture the uncaught Exceptions or Errors and record such events (my code is actually the revised version of the code in this question).
However, when I tried to use my javaagent in the testing process of an open-source project joda-time, the following error occured:
org.apache.maven.surefire.testset.TestSetFailedException: org.joda.time.TestAllPackages
at org.apache.maven.surefire.junit.JUnitTestSet.execute(JUnitTestSet.java:116)
at org.apache.maven.surefire.junit.JUnit3Provider.executeTestSet(JUnit3Provider.java:140)
at org.apache.maven.surefire.junit.JUnit3Provider.invoke(JUnit3Provider.java:113)
at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:379)
at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:340)
at org.apache.maven.surefire.booter.ForkedBooter.execute(ForkedBooter.java:125)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:413)
Caused by: java.lang.LinkageError: loader (instance of sun/misc/Launcher$AppClassLoader): attempted duplicate class definition for name: "org/joda/time/DateTimeZone"
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:468)
at java.net.URLClassLoader.access$100(URLClassLoader.java:74)
at java.net.URLClassLoader$1.run(URLClassLoader.java:369)
at java.net.URLClassLoader$1.run(URLClassLoader.java:363)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:362)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at org.joda.time.TestChronology.<clinit>(TestChronology.java:47)
at org.joda.time.TestAll.suite(TestAll.java:37)
at org.joda.time.TestAllPackages.suite(TestAllPackages.java:36)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.maven.surefire.common.junit3.JUnit3Reflector.createInstanceFromSuiteMethod(JUnit3Reflector.java:157)
at org.apache.maven.surefire.common.junit3.JUnit3Reflector.constructTestObject(JUnit3Reflector.java:124)
at org.apache.maven.surefire.junit.JUnitTestSet.execute(JUnitTestSet.java:75)
... 6 more
To my understanding, each time when sun/misc/Launcher$AppClassLoader is trying to load a class, the transform method of ClassFileTransformer is entered. The class is finally loaded after I modified the class and return the modified byte array. Therefore, every class should be loaded only once.
I further tried to print out the class name and the loader which loads it in the begining of the transform method, I found that org/joda/time/DateTimeZone appears only once, which is consistent with my understanding.
So now the only thing inconsistent with my understanding is the error. Why sun/misc/Launcher$AppClassLoader attempted duplicate class definition with my agent? Every went so soomthly when I removed my -javaagent option.
To compute frames, ASM needs to find a common super class of several classes at targets of jump instructions. To do so, ASM loads the classes to browse their hierarchy. If you load a class this way while it is also instrumented during its first load, the class will slready be loaded post instrumentation and you will end up with this error.
To avoid this, you can override the getCommonSuperClass method of ASM's ClassWriter. You'd need to parse the class files of these classes passed to the method rather then loading them. If you wanted and out-of-box implementation of this, you could use Byte Buddy which exposes ASM and resolves its ClassWriter that way.

ClassNotFoundException on runtime in a library module of my project

I have an Android project in Android Studio;
And I create a java-library module (the steps is: File -> New -> New Module -> Java library);
I add a third library by gradle, such as: "compile 'com.squareup.okhttp3:okhttp:3.9.1'";
then, I use the OkHttpClient in my Main class's main method, It ok, compile successfully.
But I run the Main method, the console print errors:
```
Exception in thread "main" java.lang.NoClassDefFoundError: okhttp3/OkHttpClient
at com.zhang.anan.mylib.myClass.main(myClass.java:12)
Caused by: java.lang.ClassNotFoundException: okhttp3.OkHttpClient
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more
```
As per the Oracle documentation, ClassNotFoundException is thrown following the failure of a class loading call, using its string name, as per below:
The Class.forName method
The ClassLoader.findSystemClass method
The ClassLoader.loadClass method
In other words, it means that one particular Java class was not found or could not be loaded at runtime from your application current context class loader.
How to resolve
Identify the caller :From the stack trace just before the Class.forName() or ClassLoader.loadClass() calls. This will help you understand if your application code is at fault vs. a third party API.
Determine if your application code is not packaged properly e.g. missing JAR file(s) from your classpath.
If the missing Java class is not from your application code, then identify if it belongs to a third party API you are using as per of your Java application. Once you identify it, you will need to add the missing JAR file(s) to your runtime classpath or web application WAR/EAR file.

Trying oracle JavaFX example but getting error

http://docs.oracle.com/javafx/2/text/NeonSign.java.html
when I try to run the neonsign java example from oracle in intelliJ this is my error:
Exception in thread "main" java.lang.ClassNotFoundException: NeonSign
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:122)
https://docs.oracle.com/javase/7/docs/api/java/lang/ClassNotFoundException.html
public class ClassNotFoundException
extends ReflectiveOperationException
Thrown when an application tries to load in a class through its string name using:
The forName method in class Class.
The findSystemClass method in class ClassLoader .
The loadClass method in class ClassLoader.
but no definition for the class with the specified name could be found.
As of release 1.4, this exception has been retrofitted to conform to the general purpose exception-chaining mechanism. The "optional exception that was raised while loading the class" that may be provided at construction time and accessed via the getException() method is now known as the cause, and may be accessed via the Throwable.getCause() method, as well as the aforementioned "legacy method."
I believe this usually happens when you do not have the Library/jar in question added, and have to add it to your current project.
Please make sure you are Using Java 8 (1.8) or you have JavaFX added to your project.
In this case, after looking through the example I found this line of code
scene.getStylesheets().add(NeonSign.class.getResource("brickStyle.css").toExternalForm());
I wonder if the issue is because it's trying to get a resource that doesn't exist (brickStyle.css). If you remove this line, does the error persist?
This might also have to do with you not having JavaFX in your project.
Also, additional reading.
How do I resolve this Java Class not found exception?
Just to add, depending on if IDEA gives you a sample of an FX application, but Netbeans has a FXApplication project with "FXMainClass" that has a built in small example.
Good luck.

Cause of java.lang.NoSuchMethodError: com.sun.jna.Native.register(Ljava/lang/String;)V

I have a java aplication and I´m moving to dynamic web projet.
The desktop application works fine, but in the dynamic web project I´m getting this error:
java.lang.NoSuchMethodError: com.sun.jna.Native.register(Ljava/lang/String;)V
at net.sourceforge.tess4j.TessAPI1.<clinit>(TessAPI1.java:44)
The line 44 in the TessAPI1 library is
Native.register(WINDOWS ? LIB_NAME : LIB_NAME_NON_WIN);
what is the possible cause of this error?
The implementation of your interface is not present or is not at the correct version. Update the implementation version where it has that method implemented.
public class NoSuchMethodError
extends IncompatibleClassChangeError
Thrown if an application tries to call a specified method of a class (either static or instance), and that class no longer has a definition of that method.
Normally, this error is caught by the compiler; this error can only occur at run time if the definition of a class has incompatibly changed.

Compiler runs class outside build path instead of class inside build path

I'm developing a java application that uses a range a parallel projects. In one of the projects I have a class that is the implementation of an interface. A problem I'm having though is that this class seems to be represented in two ways, both as a class in the build path and as a class outside the build path.
In Eclipse I can have the class open as a tab with both "path-versions", one with a "filled in J" and one with a "hollow J". This isn't a problem for me, I don't care about that, the thing is though that the compiler always seem to run the class as the "not-being-part-of-the-build-path"-version. When I debug the debugger always ends up in the class outside the build path. This causes errors and the application crashes.
I have no idea how I should solve it. In my understanding this shouldn't even be a problem, the compiler should always run classes inside the build path, right..?
Does anyone have any idea of what the problem might be?
Here's the stack trace, though I don't think it will be of much use... =\
Caused by: java.lang.NullPointerException: null
at com.smarttrust.m2m.core.services.inventory.InventoryServiceImpl.getNetworks(InventoryServiceImpl.java:244) ~[m2m-core-services-1.0.0-A-SNAPSHOT.jar:1.0.0-A-SNAPSHOT]
at com.smarttrust.m2m.ui.admin.inventory.InventoryAssignSubscription.populateDropdown(InventoryAssignSubscription.java:211) ~[classes/:na]
at com.smarttrust.m2m.ui.admin.inventory.InventoryAssignSubscription.init(InventoryAssignSubscription.java:115) ~[classes/:na]
at com.smarttrust.m2m.ui.admin.inventory.InventorySimCardTable$ActionColumnGenerator$1.buttonClick(InventorySimCardTable.java:352) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.6.0_26]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) ~[na:1.6.0_26]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) ~[na:1.6.0_26]
at java.lang.reflect.Method.invoke(Method.java:597) ~[na:1.6.0_26]
at com.vaadin.event.ListenerMethod.receiveEvent(ListenerMethod.java:490) ~[vaadin-6.6.3.jar:6.6.3]
... 23 common frames omitted
Go to project build path, in Order and Export tab.
you see all source codes which is related to your project, You should move up the class that is right (as you say the one which is in build path)

Categories