Class not found: org.springframework.context.ApplicationContext - java

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.

Related

ClassNotFoundException: com.azure.storage.common.policy.RequestRetryOptions

I am trying to run a java program that sends and receives messages to and from Azure Storage Queues; however, I keep getting the following ClassNotFoundException at runtime. I am having a hard time finding the jars necessary to fix it. Nothing I have found so far actually solves the problem.
Exception in thread "main" java.lang.NoClassDefFoundError: com/azure/storage/common/policy/RequestRetryOptions
at com.azure.storage.queue.QueueServiceClientBuilder.<init>(QueueServiceClientBuilder.java:83)
at com.cloudproject.azure.Connector.initiateConnection(Connector.java:58)
at com.cloudproject.server.ConnectionServer.main(ConnectionServer.java:30)
Caused by: java.lang.ClassNotFoundException: com.azure.storage.common.policy.RequestRetryOptions
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352)
at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
... 3 more
I am not using maven in the build process, though I have pulled several jars from the maven repository - none of which have helped.
This seems to have been fixed by including the azure-storage-common jar, which I pulled from here. Not sure how I overlooked this one earlier.

OSGi java.lang.NoClassDefFoundError: org/osgi/framework/BundleContext

I'm new with OSGi and I'm having trouble trying to run an example of this library: https://github.com/jitsi/libjitsi
java.lang.NoClassDefFoundError: org/osgi/framework/BundleContext
at java.lang.Class.getDeclaredConstructors0(Native Method)
at java.lang.Class.privateGetDeclaredConstructors(Class.java:2671)
at java.lang.Class.getConstructor0(Class.java:3075)
at java.lang.Class.newInstance(Class.java:412)
at org.jitsi.service.libjitsi.LibJitsi.start(LibJitsi.java:247)
at org.jitsi.service.libjitsi.LibJitsi.start(LibJitsi.java:180)
at com.dotsystem.AVTransmit2.main(AVTransmit2.java:327)
Caused by: java.lang.ClassNotFoundException: org.osgi.framework.BundleContext
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 7 more
Do I need an external jar for osgi?
A class not found exception on a core osgi class, seems that you are missing a jar/framework jar that implements OSGi specification. Check which OSGi implementation your target system supports. If you have choice in choosing you can try,
Apache felix OSGi
Eclipse equinox OSGi
If you are trying to run example from IntelliJ it may be problem that you haven`t imported maven dependencies. When you import java project into IntelliJ you must also import its dependencies (they are not imported by default).

java.lang.ClassNotFoundException during gradle build

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

ClassNotFoundException: org.apache.felix.dm.DependencyActivatorBase with Pax Exam

I am using Pax Exam (3.5.0) for my OSGi unit tests. I have created a test but when I run it I get this stacktrace:
java.lang.NoClassDefFoundError: org/apache/felix/dm/DependencyActivatorBase
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.findClass(BundleWiringImpl.java:2279)
at org.apache.felix.framework.BundleWiringImpl.findClassOrResourceByDelegation(BundleWiringImpl.java:1501)
at org.apache.felix.framework.BundleWiringImpl.access$400(BundleWiringImpl.java:75)
at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.loadClass(BundleWiringImpl.java:1955)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at org.apache.felix.framework.BundleWiringImpl.getClassByDelegation(BundleWiringImpl.java:1374)
at org.apache.felix.framework.Felix.createBundleActivator(Felix.java:4329)
at org.apache.felix.framework.Felix.activateBundle(Felix.java:2141)
at org.apache.felix.framework.Felix.startBundle(Felix.java:2064)
at org.apache.felix.framework.Felix.setActiveStartLevel(Felix.java:1291)
at org.apache.felix.framework.FrameworkStartLevelImpl.run(FrameworkStartLevelImpl.java:304)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.ClassNotFoundException: org.apache.felix.dm.DependencyActivatorBase not found by com.company.wine.infrastructure [19]
at org.apache.felix.framework.BundleWiringImpl.findClassOrResourceByDelegation(BundleWiringImpl.java:1532)
at org.apache.felix.framework.BundleWiringImpl.access$400(BundleWiringImpl.java:75)
at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.loadClass(BundleWiringImpl.java:1955)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 14 more
I have configured CoreOptions.mavenBundle("org.apache.felix", "org.apache.felix.dependencymanager", "3.1.0") in the options and I see that org.ops4j.pax.logging.pax-logging-api[org.ops4j.pax.swissbox.extender.BundleWatcher] : Scanning bundle [org.apache.felix.dependencymanager] is loaded.
Still I get the exception. Anyone a clue on what I am doing wrong?
Cheers.
You have to differentiate in the way Pax Exam is designed and how you used it.
Pax Exams main use case is to use it in an integration test where you test a previously build artifact.
If you want to test it in the same module you have two ways of doing so.
1) in the Integrationtest-Phase of maven by referencing the freshly build artifact via a file: dependency.
2) by building a tiny-bundle containing your testable classes and other artifacts. This will need to also alter the Package-Imports/Exports in your test.
A sample of doing this can be found here

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