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.
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.
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.
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.
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.
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
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With