Java ClassNotFoundException with maven dependency
Asked Answered
F

5

48

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?

Festinate answered 10/10, 2012 at 3:5 Comment(0)
A
37

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.

Avaricious answered 10/10, 2012 at 3:8 Comment(1)
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.Festinate
B
10
<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.

Bismuth answered 10/10, 2012 at 3:8 Comment(0)
D
1

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.

Delrio answered 21/4, 2017 at 10:52 Comment(0)
D
0

Answer somewhat related to your problem, but still can help others.
Adding <scope>compile</scope> to the dependency was not enough in my case. I also had to add <packaging>jar</packaging> to the target module's pom.

Daredeviltry answered 6/2, 2019 at 13:57 Comment(0)
T
0

I was also facing the same problem, after trying various solutions the issue was resolved by following steps...

  1. click on server >open launch configurations >Arguments (copy "LocalPath\temp0\wtpwebapps" path).

  2. go to "LocalPath\temp0\wtpwebapps\ProjectName\WEB-INF\lib".

  3. copy the JAR (which was causing ClassNotFoundException) from .m2 and place it in "LocalPath\temp0\wtpwebapps\ProjectName\WEB-INF\lib" path.

Tichon answered 14/7, 2021 at 15:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.