Groovy class not found exception by thirdparty java class during run-app - java

We are using a third party tool (Snowbound docViewer) which is expected to execute application's class file which is passed to the tool as a parameter. But this tool is not able to find the class passed to it only when we do run-app, it works with the war file. However same works when war is deployed to standalone tomcat server. May be class loader used to execute tool is different from our application.
I am trying to load the class like
File file = Locator.getClassSource(DocumentContentHandler)
def classLoader = BootStrap.class.classLoader.rootLoader
String path = Locator.fileToURL(file)
classLoader.addURL(new URL(path +'com/mycompany/snowbound/DocumentContentHandler.class'));
Url value generated is :
"file:/Users/amitjain/projects/mycompany/target/classes/com/mycompany/snowbound/DocumentContentHandler.class"
The exception I get is
java.lang.ClassNotFoundException: com.mycompany.snowbound.DocumentContentHandler
at org.codehaus.groovy.tools.RootLoader.findClass(RootLoader.java:175)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at org.codehaus.groovy.tools.RootLoader.loadClass(RootLoader.java:147)
at org.codehaus.groovy.grails.cli.support.GrailsRootLoader.loadClass(GrailsRootLoader.java:51)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:190)
at com.snowbound.clientcontentserver.RetrievalProcessorConfigurator.initContentClass(RetrievalProcessorConfigurator.java:977)
Even when I do the following I get class not found exception.
Class.forName('com.mycompany.snowbound.DocumentContentHandler', true, BootStrap.class.classLoader.parent)
What can I do here? Thanks,

Related

java.lang.NoClassDefFoundError on Docker

When The Program is Ran in the Local PC, it is working fine, but if it is running in the docker throws the following error:
Exception in thread "Thread-16" java.lang.NoClassDefFoundError: org/apache/kafka/common/serialization/Serializer
at org.eclipse.kura.example.configurable.ConfigurableExample$1.run(ConfigurableExample.java:52)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.ClassNotFoundException: org.apache.kafka.common.serialization.Serializer cannot be found by org.eclipse.kura.example.configurable_1.0.0.202201251713
at org.eclipse.osgi.internal.loader.BundleLoader.findClassInternal(BundleLoader.java:484)
at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:395)
at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:387)
at org.eclipse.osgi.internal.loader.ModuleClassLoader.loadClass(ModuleClassLoader.java:150)
at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
... 2 more
But this is working fine in the LocalMachine.
What is the issue ?
This may be debugged in following ways:
Ensure that you have all the required dependencies inside your docker container. You may want to check your docker base image first.
Check if you have set the classpath appropriately to include that jar file (kafka-clients).
Validate your entrypoint/cmd which you use to start the service to check if it has the appropriate classpath.
Check your classloader and see why it is not able to load that class.
If your executable is a fat jar file, check if that jar file contains the kafka-clients library where that class is present.

JNI NoClassDefFoundError in springboot executable jar

I have springboot application which is connecting to client shared dynamic library using jni.
There is one **callback method **which is registered to dll using ine register function.
This method is invoked on some events from dll.
When executed as java application from intellij, this works fine but when executed **as jar **and when this callback method is invoked its throwing **NoClassDefFoundError ** as below
java.lang.NoClassDefFoundError: com/test/Sample
Caused by: java.lang.ClassNotFoundException: com.test.Sample
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
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)
By default springboot puts all classes inside BOOT-INF folder.
When I manually modified jar and added this class outside BOOT-INF i.e at rootLevel it worked.
Like below:
BOOT-INF
com
META-INF
org
In above snippet com is directory inside which Sample class was present under subpackage test.
springboot version used: 2.3.6.RELEASE
Using springboot-maven-plugin to create executable jar

java.lang.ClassNotFoundException while instrumenting eclipse rcp application

I tried injecting a method call statement using javassist in every method of some desired classes. The statement corresponds to a class in the java-agent jar. For example if my agent has class like a.b.HelperClass, I am injecting a.b.HelperClass.call(); in the beginning of every class using javassist.
I tried with a spring boot web app and the agent works fine but with RCP application it is throwing
java.lang.ClassNotFoundException: a.b.HelperClass
at org.eclipse.osgi.internal.loader.BundleLoader.findClassInternal(BundleLoader.java:506)
at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:422)
at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:410)
at org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader.loadClass(DefaultClassLoader.java:107)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
Do I need to load the classes residing inside agent-jar using the eclipse class loader? BTW I'm instrumenting by calling agent-main method and not premain.
Eclipse is OSGi based and has a module system that is enforced.
The HelperClass must be in the classpath of the Eclipse Plugin where it should be used.

Using javaagent with javassist in WildFly

I've written a simple javaagent that basically injects some logging in classes that are part of the JDK, such as javax/imageio/spi/ServiceRegistry, using javassist.
It works fine in a simple standalone application, but I cannot get it to work in WildFly 10. What I have tried:
Added the agent JAR as a module to WildFly along with the corresponding module.xml which declares <module name="org.javassist"/> as dependency
Appended the agent package and org.javassist to JBOSS_MODULES_SYSTEM_PKGS
Appended -javaagent:/full/path/to/agent/jar/in/module to JAVA_OPTS
WildFly starts but in server.log I get:
java.lang.NoClassDefFoundError: javassist/ClassPool
at net.luniks.agent.LoggerChanger.transform(LoggerChanger.java:48)
at sun.instrument.TransformerManager.transform(TransformerManager.java:188)
at sun.instrument.InstrumentationImpl.transform(InstrumentationImpl.java:428)
at java.lang.ClassLoader.findBootstrapClass(Native Method)
at java.lang.ClassLoader.findBootstrapClassOrNull(ClassLoader.java:1015)
at java.lang.ClassLoader.loadClass(ClassLoader.java:413)
at java.lang.ClassLoader.loadClass(ClassLoader.java:411)
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:348)
at org.jboss.modules.ClassLoaderLocalLoader.loadClassLocal(ClassLoaderLocalLoader.java:74)
at org.jboss.modules.Module.loadModuleClass(Module.java:606)
at org.jboss.modules.ModuleClassLoader.findClass(ModuleClassLoader.java:190)
It seems the module class loader is used but even though the agent module has a dependency to org.javassist, it still cannot load its classes.
What am I missing?
Eventually I got it to work by adding only the package of the agent to JBOSS_MODULES_SYSTEM_PKGS (not "javassist") and adding the org.javassist module JAR to the boot classpath:
JAVA_OPTS="$JAVA_OPTS -Xbootclasspath/p:/path/to/wildfly/modules/system/layers/base/org/javassist/main/javassist-3.18.1-GA.jar"
Now the agent works, it can load the Javassist classes. Certainly not the clean way but it is just for debugging purposes...

Java Class Loader issue/Maven issue in multiple Jetty container

I have a big appliance running using multiple application. I have multiple Jetty containers where multiple applications are deployed to each container.. Now, i am facing a simple class not found warning while deploying the application.
This is my analysis:
JettyContainer_1 contain App_1 which has many common JAR files which can be shared across all other containers so all applications can access these JAR files.
Now, the problems is that a spring framework class in shared libs in JettyContainer_1 is trying to load a class "ClassAInContainer3"(from jar file) from App_X in container 3. Then am getting the class not found error..
Here is the stack trace..
Caused by: java.lang.ClassNotFoundException: com.test.ClassAInContainer3
at java.net.URLClassLoader$1.run(URLClassLoader.java:366) ~[na:1.7.0_55]
at java.net.URLClassLoader$1.run(URLClassLoader.java:355) ~[na:1.7.0_55]
at java.security.AccessController.doPrivileged(Native Method) ~[na:1.7.0_55]
at java.net.URLClassLoader.findClass(URLClassLoader.java:354) ~[na:1.7.0_55]
at java.lang.ClassLoader.loadClass(ClassLoader.java:425) ~[na:1.7.0_55]
at java.lang.ClassLoader.loadClass(ClassLoader.java:358) ~[na:1.7.0_55]
at org.springframework.util.ClassUtils.forName(ClassUtils.java:258) ~[spring-core.jar:3.2.0.RELEASE]
at org.springframework.amqp.support.converter.DefaultJavaTypeMapper.getClassIdType(DefaultJavaTypeMapper.java:82) ~[spring-amqp.jar:na]
... 16 common frames omitted
2015-08-19 05:38:16.123 UTC,WARN ,App1-web,messaging,com.test.Rabbit,null,SimpleAsyncTaskExecutor-1,Caught an exception while handling a rabbitmq message, Publisher/Consumer should have handled this.
org.springframework.amqp.support.converter.MessageConversionException: failed to resolve class name. Class not found [com.test.ClassAInContainer3]
at org.springframework.amqp.support.converter.DefaultJavaTypeMapper.getClassIdType(DefaultJavaTypeMapper.java:85) ~[spring-amqp.jar:na]
at org.springframework.amqp.support.converter.DefaultJavaTypeMapper.toJavaType(DefaultJavaTypeMapper.java:53) ~[spring-amqp.jar:na]
at org.springframework.amqp.support.converter.JsonMessageConverter.fromMessage(JsonMessageConverter.java:117) ~[spring-amqp.jar:na]
Any inputs on how to resolve this.. I am using Maven for building the application.. I suspect this is becoz since both libs are in different location, the spring class(present in jettyContainer1) cannot load the ClassAInContainer3 from App_3. So may be we can add some dependencies or class loader configurations..
Any inputs will be much appreciated.. Thanks in advance.. :)
The exception makes sense if the "spring framework class" is loaded by the Jetty class loader and the ClassAInContainer3 is loaded by the WebAppClassLoader (typically so, if it is in WEB-INF/lib or WEB-INF/classes of a webapp).
I started a Jetty server in debug and executed the following commands (in the Display view in Eclipse) while blocked in a breack point inside a web controller (i.e. I'm executing code loaded by a WebAppClassLoader):
this.getClass().getClassLoader()
org.eclipse.jetty.webapp.WebAppClassLoader) WebAppClassLoader=653648989#26f5e45d
If I try to climb the class loader hierarchy, I get the server class loader of Jetty:
this.getClass().getClassLoader().getParent()
(org.codehaus.plexus.classworlds.realm.ClassRealm) ClassRealm[plugin>org.eclipse.jetty:jetty-maven-plugin:9.3.2.v20150730, parent: sun.misc.Launcher$AppClassLoader#58644d46]
this is the root class loader, beyond this point I cannot go, in fact:
this.getClass().getClassLoader().getParent().getParent()
null
Now, if I invoke loadClass in the code of a class that's been loaded by the AppClassLoader (at server startup, typically), It will use its own class loader first (that is, the AppClassLoader itself) and then, if the class is not found, the parent class loader (because the servlet container will use a "parent last" loading strategy, but note that, as I stated before, there is no other class loader beyond AppClassLoader).
But it is perfectly feasible that the ClassAInContainer3 is not reachable by the AppClassLoader, because it is not in the classpath that is known to it. In fact look what happens if I try to load an instance of my web controller (a class contained in WEB-INF/classes) with the AppClassLoader:
this.getClass().getClassLoader().getParent().loadClass("org.test.showcase.HomeController")
Evaluation failed. Reason(s):
An exception occurred: java.lang.ClassNotFoundException
Exactly what I suspect is happening in your case.

Categories

Resources