Spring java.lang.NoClassDefFoundError - java

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");

Related

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.

Error calling to the cloud jclouds

So I am trying to download files on a rackspace server to my local server. It is laid out here. For some odd reason, I am trying to make the initial call to the cloud and it is giving me some serious feedback. No idea where to go from here. I've tried all sorts of different Maven dependencies to fix the problem, but none have come close. Am I overlooking something? TIA!
My code:
public static void main (String[] args){
CloudFilesApi cloudFilesApi = ContextBuilder.newBuilder(PROVIDER)
.credentials(USERNAME, KEY)
.buildApi(CloudFilesApi.class);
}
And here are my errors (without Guice):
Exception in thread "main" java.lang.NoClassDefFoundError: com/google/inject/Stage
at org.jclouds.ContextBuilder.<clinit>(ContextBuilder.java:151)
at com.unidev.download.Main.main(Main.java:10)
Caused by: java.lang.ClassNotFoundException: com.google.inject.Stage
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)
... 2 more
Here are my dependencies (now):
<dependencies>
<!-- Rackspace US dependencies -->
<dependency>
<groupId>org.apache.jclouds.provider</groupId>
<artifactId>rackspace-cloudfiles-us</artifactId>
<version>${jclouds.version}</version>
</dependency>
Code snippet for dependency tree(updated): https://codeshare.io/bfbKg
I guess it is a Maven issue... When I was building the Maven project and using the dependencies via the pom file, it was somehow building incorrectly. Instead, I started a Java file and built a lib folder, pulled all the jars I needed and then just tied them in via the build path... and it is working well now...

NoClassDefFoundError with Maven and a Java Wiki Library

I'm using a library to make Wikipedia API calls, called the Java Wiki Bot Framework. I'm also using Maven for builds, and I've added the framework to the pom.xml just as the github page says. However, when I try to instantiate any of the classes in the library, it gives me this error:
Exception in thread "main" java.lang.NoClassDefFoundError: net/sourceforge/jwbf/core/actions/HttpActionClient
at com.FactFinder.WikiParser.makeBot(WikiParser.java:21)
at com.FactFinder.WikiParser.<init>(WikiParser.java:16)
at com.FactFinder.App.main(App.java:15)
Caused by: java.lang.ClassNotFoundException: net.sourceforge.jwbf.core.actions.HttpActionClient
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)
... 3 more
My pom.xml
Apparently this is an error that crops up when your jar can't find a class, but I've checked the classpath, and it looks fine. The jar is included in the proper folder, and I've verified that it includes the classes I tried to use. I considered contacting the library author, but I figured its probably more my problem than his.
Why are you using the Snapshot version 4.0.0-SNAPSHOT? I would strongly suggest to use a final version to avoid issues, of which 3.1.0 seems to be the latest and most popular one:
<dependency>
<groupId>net.sourceforge</groupId>
<artifactId>jwbf</artifactId>
<version>3.1.0</version>
</dependency>
[UPDATE]
Follow naming conventions; package name: com.factfinder (all lowercase). Fix in both your code, pom, and directory names.
Add the version for the maven-jar-plugin
Note that I tried using your pom - it seemed to work for me ...
Are you sure you don't get any other warnings or errors during your maven build? can you do a clean package?
Check line 53 of your POM:
<classpathPrefix>lib\</classpathPrefix>
Should be:
<classpathPrefix>lib/</classpathPrefix>

How to prevent jar-in-jar-loader loading specific class?

Imagine I have a following project structure of folders
A
B
C
and many external jars.
Now I am trying to build two different executable-jars like this A+B, A+C.
At first, I do Eclipse -> Export -> Executable Jar and save script for Ant. So it generates Ant-script and jar-in-jar-loader. Now I modify ant script to exclude folder B
<fileset dir="C:/Users/Nikolay/bin">
<exclude name="org/B"/>
</fileset>
and keep jar-in-jar-loader.zip file unmodified.
After building the jar running gives this error:
Exception in thread "main" java.lang.NoClassDefFoundError: org/B
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoa
der.java:56)
Caused by: java.lang.ClassNotFoundException: org.B
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:423)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
... 3 more
But I am sure B class is not used in the A.
It seems like JarRscrLoader tries to load all listed classes at application startup. How can I prevent that?
Please, don't suggest me to split the code at Eclipse into 2-3 projects.
Create two different Run configurations (Run --> Run configurations...) and modify the included libraries in the classpath tab according to your requirements.
When you export the project, just select the appropriate configuration.

java.lang.NoClassDefFoundError

I got "java.lang.NoClassDefFoundError" Exception when I want to use a certain jar file.
Exception in thread "main" java.lang.NoClassDefFoundError: .
org/apache/http/client/ClientProtocolException
at ?uk.org.taverna.server.client.connection.ConnectionFactory.getConnection(ConnectionFactory.java:63)
at uk.org.taverna.server.client.Server.<init>(Server.java:99)
at uk.org.taverna.server.client.Server.<init>(Server.java:126)
at uk.org.taverna.server.client.Server.connect(Server.java:293)
at uk.org.taverna.server.usage.ServerUsage.Usage(ServerUsage.java:24)
at Test.main(Test.java:23)
Caused by: java.lang.ClassNotFoundException: org.apache.http.client.ClientProtocolException
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:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
I import this jar file to jar library, when I call the method in that jar file I got this Exception. By the way, This jar file be wrote by myself, and I can run it correctly.
I have no idea how to solve this problem, I am quite new in java.
Thanks in advance.
Sandy
You need also at least HttpClient jar-library. You can get it at Apache Commons web-site.
Jar libraries may and often refer to another jar libraries. In your case library you are using refers to HttpClient library from Apache Commons framework. You need to add this library to your project.
You should but all the jar files you use in you application in the libs folder in your application directory .
so if you use the apachecommons jar file copy and paste the jar file to the libs folder in your application .

Categories

Resources