java.lang.ClassNotFoundException during gradle build - java

I'm working on a java project with spring boot, and building it with gradle. I was successfully able to run 'gradle build'. But after than when I run 'java -jar build/libs/gs-spring-boot-0.1.0.jar', it throws me java.lang.ClassNotFoundException error. It searches for mainClassName attribute in the build.gradle file. I tries setting it up, still it gives me the same error.
What am I doing wrong?
Few more details:
my package name: com.foo.bar
file that contains main method: FileData.java
I am adding 'mainClassName = 'com.foo.bar.FileData' in my build.gradle file.
Error it throws:
java.lang.ClassNotFoundException: com.foo.bar.FileData
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at org.springframework.boot.loader.LaunchedURLClassLoader.doLoadClass(LaunchedURLClassLoader.java:177)
at org.springframework.boot.loader.LaunchedURLClassLoader.loadClass(LaunchedURLClassLoader.java:143)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:46)
at java.lang.Thread.run(Thread.java:745)
Thanks for your help

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.

Class not found: org.springframework.context.ApplicationContext

I am new on Spring Framework. I had followed by instructions given in book Spring 4 for Professionals. In book author configured framework via xml, but I want configure it with Java. While running I'm getting ClassNotFoundException
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/context/ApplicationContext
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
at java.lang.Class.getMethod0(Class.java:3018)
at java.lang.Class.getMethod(Class.java:1784)
at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
Caused by: java.lang.ClassNotFoundException: org.springframework.context.ApplicationContext
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)
... 7 more
I have published my code in
github.
What I'm doing wrong?
Works fine here, Hello World! is printed when I run it.
How do you try to run it?
I guess you tried to do java -jar spring-framework-example.jar.
This of course does not work because you miss the libraries in your classpath then.
If you exchange
jar {
manifest {
attributes 'Main-Class': 'com.bakhtiyor.sample.Application'
}
}
with
apply plugin: 'application'
mainClassName = 'com.bakhtiyor.sample.Application'
and then use gradle run, the application works fine.
PS: You should always use the Gradle Wrapper, even in the tiniest project. Then it is easier for others to build your project because nothing needs to be installed to build and the build always run with the Gradle version the build is designed for.

java.lang.NoClassDefFoundError after build Success in maven

Application Build got successful but getting the following error :
Exception in thread "Thread-29" java.lang.NoClassDefFoundError: org/apache/hadoop/util/ShutdownHookManager$2
at org.apache.hadoop.util.ShutdownHookManager.getShutdownHooksInOrder(ShutdownHookManager.java:124)
at org.apache.hadoop.util.ShutdownHookManager$1.run(ShutdownHookManager.java:52)
Caused by: java.lang.ClassNotFoundException: org.apache.hadoop.util.ShutdownHookManager$2
at org.codehaus.plexus.classworlds.strategy.SelfFirstStrategy.loadClass(SelfFirstStrategy.java:50)
at org.codehaus.plexus.classworlds.realm.ClassRealm.unsynchronizedLoadClass(ClassRealm.java:271)
at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClass(ClassRealm.java:247)
at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClass(ClassRealm.java:239)
... 2 more
I added this hadoop-common jar which has this class in pom file, classpath or any possible way i knew and could google but nothing worked.
It looks like a runtime problem. What I understand is that ClassRealm is trying to load a class at runtime and that class is not present in the jar. Just give it a look to the SelfFirstStrategy.java:50 and check if the class that is trying to be loaded actually exists.

java.lang.NoClassDefFoundError: org/apache/xmlbeans/XmlException

I am trying to build my Java project with Ant.
But I am getting java.lang.NoClassDefFoundError: org/apache/xmlbeans/XmlException while executing the cleintgen Ant task.
I am not sure if I am missing any jar. I have added jars mentioned below in taskdef classpath attribute-
weblogic-classes.jar
wlthint3client.jar
wlfullclient.jar
xmlbeans-1.0.jar
Error at line -
<clientgenSRF wsdl="D:/POC/Code/WSDLs/Update_V3.wsdl"
destFile="D:/POC/Code/SRF/clientstubs/newjar/createTroubleTicket_client.jar" />
Possibly missing in older version.
Upgrade xmlbeans-1.0.jar to xmlbeans-2.6.0.jar will be advantages.

Spring java.lang.NoClassDefFoundError

I'm totally a newbie in springs so I wanted to try a helloworld program in springs. I came across this tutorial.
I downloaded the source code with library provided on this site, I have the latest Spring STS installed on my Eclipse Kepler. I got this error :
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
at org.springframework.context.support.AbstractApplicationContext.(AbstractApplicationContext.java:153)
at org.springframework.context.support.AbstractApplicationContext.(AbstractApplicationContext.java:217)
at org.springframework.context.support.AbstractRefreshableApplicationContext.(AbstractRefreshableApplicationContext.java:88)
at org.springframework.context.support.AbstractRefreshableConfigApplicationContext.(AbstractRefreshableConfigApplicationContext.java:58)
at org.springframework.context.support.AbstractXmlApplicationContext.(AbstractXmlApplicationContext.java:61)
at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:136)
at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:83)
at com.vaannila.HelloWorldApp.main(HelloWorldApp.java:9)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 8 more
I also tried manually doing the entire program with a different class name and It throws the same error.
Things I have tried:
built path and included the springframework jars
also included commons-logging-1.1.1.jar
I got past this exception by adding commons-logging-1.1.1.jar to the classpath of my project. I had it in my maven repository from other projects.
Plus, to make the example work, the file name for the instantiation of ClassPathXmlApplicationContext needs to be qualified with the package path. If you are following the example exactly, you need to instantiate your context like this:
ApplicationContext context = new ClassPathXmlApplicationContext("com/vaannila/beans.xml");

Categories

Resources