Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.NoClassDefFoundError: in eclipse maven

In eclipse with maven, I have add a dependency as a local jar file, as like this:

<dependency>
    <groupId>xyz-core</groupId>
    <artifactId>xyz-core</artifactId>
    <version>0</version>
    <scope>system</scope>
    <systemPath>/home/xyz/xyz-core.jar</systemPath>
</dependency>

In this jar file I have a interface that is using in my application.

When I run my application on tomcat server It show exception for that interface

Exception sending context initialized event to listener instance of class
org.springframework.web.context.ContextLoaderListener
java.lang.NoClassDefFoundError: com/mxgraph/canvas/mxICanvas2D

while mxICanvas2D is a interface.

like image 342
Prashant Aggarwal Avatar asked Sep 07 '13 07:09

Prashant Aggarwal


People also ask

What is Java NoClassDefFoundError?

What Is java.lang.NoClassDefFoundError? When the Java Runtime runs a Java program, it does not load all the classes and dependencies at once. Instead, it calls upon the Java Classloader to load classes in memory as-and-when-required. While loading a class, if the Classloader cannot find the class's definition, it throws the NoClassDefFoundError.

How do I set the class path of a Maven project?

1) Pretty much anything you run through maven, including the exec plugin, will automatically set up the class path for you based on your project's dependencies as specified in the pom. That's a major reason for maven's popularity. 2) Yes, you have to run at least mvn compile prior to running the exec plugin.

Why does the classloader throw NoClassDefFoundError?

While loading a class, if the Classloader cannot find the class's definition, it throws the NoClassDefFoundError. There are a couple of reasons for which Java cannot find the class's definition, which are: Missing a few dependent jars which is the most common reason. All jars are added as dependencies but in the wrong path.

Why can't I find the class's definition in Java?

There are a couple of reasons for which Java cannot find the class's definition, which are: Missing a few dependent jars which is the most common reason. All jars are added as dependencies but in the wrong path. Version mismatches in the dependencies.


2 Answers

This is most likely because you have set the scope to system. According to the Maven documentation:

system

This scope is similar to provided except that you have to provide the JAR which contains it explicitly. The artifact is always available and is not looked up in a repository.

In other words, the dependency is not put on your classpath when you run your application if you use system; you have to do that yourself.

Use one of the other scopes, for example compile.

like image 148
Jesper Avatar answered Sep 21 '22 20:09

Jesper


Have you added "Maven Dependencies" to the project's "Web Deployment Assembly". If not, add that as follows:

Right Click on your project -> Properties -> Deployment Assembly -> Add -> Java Build Path Entries -> Next and then from there you can add "maven Dependencies". Then build and try to run your app.

like image 28
Debojit Saikia Avatar answered Sep 21 '22 20:09

Debojit Saikia