java.lang.NoClassDefFoundError: in eclipse maven

17,802

Solution 1

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.

Solution 2

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.

Solution 3

Since you are using system scope it means that maven will use that to compile your project, you will not see your errors, however, when you are running your application in tomcat, that is not related to maven and tomact does not know where your dependencies are, one way to solve it is copy the needed .jars to your tomcat/lib folder.

Usually you will like to have provided or compile scope, however these scopes are used if you have a repository. When you create a new mavenized project and then build it, maven will create a .jar for that particular dependency in your local machine (c:/users/user/.m2/repository/). That will work for your own projects.

Share:
17,802
Prashant Aggarwal
Author by

Prashant Aggarwal

working as a software Engineer in java/J2ee

Updated on June 09, 2022

Comments

  • Prashant Aggarwal
    Prashant Aggarwal almost 2 years

    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.

  • Prashant Aggarwal
    Prashant Aggarwal over 10 years
    it will give error in pom.xml while adding scope as complie Project build error: 'dependencies.dependency.systemPath' for xyz-core:xyz-core:jar must be omitted. This field may only be specified for a dependency with system scope.
  • Jesper
    Jesper over 10 years
    Remove the systemPath element from the dependency, and install the dependency in your Maven repository instead.
  • Jesper
    Jesper over 10 years
    Good! You can click the checkmark at the top left of my answer to accept it as the answer to your question.
  • Jesper
    Jesper almost 7 years
    @PhilipRego It doesn't work for you, because you have a different problem, apparently. That doesn't mean my answer is bad and doesn't work in general. Note that the OP mentioned it solved his problem... Post a new question and explain your own problem.
  • Jesper
    Jesper almost 7 years
    @PhilipRego Yes he did, see the third comment to this answer. And if you don't have <scope>system</scope> in your pom.xml then you do not have the same problem.
  • pstanton
    pstanton over 5 years
    mine was scope=test .. for some stupid reason the mavenrepository eg dependency had this set.