Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 922
Vex Avatar asked Oct 10 '12 03:10

Vex


People also ask

Why do we get ClassNotFoundException in Java?

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.

Can we catch ClassNotFoundException in Java?

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.

How does Maven build in VS code?

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.


2 Answers

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.

like image 169
jmj Avatar answered Sep 21 '22 06:09

jmj


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

like image 25
Vikdor Avatar answered Sep 19 '22 06:09

Vikdor