I am getting ClassNotFoundException
and NoClassDefFoundError
exceptions when I attempt to run my application using a maven defined dependency.
I added my maven dependency for the jar in question to my pom.xml
file with the following declaration:
<dependency> <groupId>spy</groupId> <artifactId>spymemcached</artifactId> <version>2.8.4</version> <scope>provided</scope> </dependency>
This added the relevant JAR file to my Maven Dependencies folder in Eclipse. I can access the classes in code but I get the mentioned exceptions once I run the application.
The jar is referenced in my Java build path under Maven dependencies:
My local maven repository is added to my classpath:
When I attempt to run the application, I get the following two exceptions:
java.lang.NoClassDefFoundError: Lnet/spy/memcached/MemcachedClient; java.lang.ClassNotFoundException: net.spy.memcached.MemcachedClient
Can anyone point me in the right direction?
ClassNotFoundException is a checked exception which occurs when an application tries to load a class through its fully-qualified name and can not find its definition on the classpath. This occurs mainly when trying to load classes using Class.
ClassNotFoundException is a checked exception, so it has to be catch or thrown to the caller. ClassNotFoundException always occurs at runtime because we are indirectly loading the class using Classloader. Java compiler has no way to know if the class will be present in the classpath at runtime or not.
There are several ways to create a Maven project: From the Maven Explorer, select the + Create Maven Project button. Open the Command Palette (Ctrl+Shift+P), search for Create Java Project command. Right-click on a target folder and select Create Maven Project.
Change provided
to compile
Provided
This is much like compile, but indicates you expect the JDK or a container to provide the dependency at runtime. For example, when building a web application for the Java Enterprise Edition, you would set the dependency on the Servlet API and related Java EE APIs to scope provided because the web container provides those classes. This scope is only available on the compilation and test classpath, and is not transitive.
<scope>provided</scope>
"Provided" scope implies that the dependencies should be available only during compile phase and they will be available elsewhere during runtime and Maven shouldn't package them with the rest of the jars and classes of the current application.
Your dependency doesn't seem to be of "provided" scope. Remove that scope from your dependency definition and the jars will be present in your packaged jar/war/ear.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With