Search code examples
javamavennoclassdeffounderrorclassnotfoundexception

Java ClassNotFoundException with maven dependency


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:

Java build path

My local maven repository is added to my classpath:

screenshot

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?


Solution

  • 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.