Java ClassNotFoundException with maven dependency

73,170

Solution 1

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.

Solution 2

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

Solution 3

I was facing the same issue but i didn't have the tag defined on my POM, it was always working fine until one day all of a sudden started giving me that error in my local machine for no reason, the dependency was correctly set up in the POM and the jar was present in the local maven repository.

I tried cleaning the project and updating maven project but nothing, none of the other solutions suggested on other posts worked for me either.

I was finally able to solve it by going to the servers tab -> right click on Tomcat v8.0 -> browse deployment location

this should lead you to a temp folder like

.metadata.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps

then you browse to your project folder -> WEB-INF -> lib -> here i found out that the jar from the library that was giving the error was missing, so i just copied it from .m2\repository

Restarted the server and it started working as usual again.

I hope this helps some body facing the same issue.

Share:
73,170

Related videos on Youtube

Vex
Author by

Vex

Updated on July 09, 2022

Comments

  • Vex
    Vex almost 2 years

    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?

  • Vex
    Vex over 11 years
    Thanks very much, this indeed worked. I copied that declaration verbatim from their website and didn't think twice about it; I don't know why they declared it like that.