Vex
Vex

Reputation: 1239

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?

Upvotes: 49

Views: 88297

Answers (5)

shubham khojare
shubham khojare

Reputation: 1

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.

Upvotes: -1

adamjankovec
adamjankovec

Reputation: 1

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.

Upvotes: 0

MFJ
MFJ

Reputation: 51

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.

Upvotes: 1

Vikdor
Vikdor

Reputation: 24134

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

Upvotes: 10

Jigar Joshi
Jigar Joshi

Reputation: 240946

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.

Upvotes: 36

Related Questions