Skip to content
Advertisement

Maven: java.lang.NoClassDefFoundErrorexception, but jars are downloaded in .m2

I have a tomcat project: tomcat/webapps/Project. That project makes use of org.json library, the dependency is written in pom.xml file:

<dependency>
    <groupId>org.json</groupId>
    <artifactId>json</artifactId>
    <version>20180813</version>
</dependency>

I have installed maven on my server and run this command, while in the Project directory: mvn install

Then I checked /root/.m2/repository/org/json/20180813 folder and the file json-20180813.jar is there!

However when I restart my server: shutdown.sh and try to make use of my app, the error is still there:

javax.servlet.ServletException: java.lang.NoClassDefFoundError: org/json/JSONObject

Why does this happen and how to fix it?

EDIT: People pointed out the copying of the jar to tomcat/lib directory. I did it and it worked. However, I would also be interested in a more permanent solution, so that I wouldn’t be forced to do this again with other jars in the future.

Advertisement

Answer

Command mvn install will compile and install your project library to your local repository. In addition it will download and copy any dependencies there also, under that .m2/repository directory. See this question for more detailed explanation about mvn install.

However it has nothing to do with Tomcat or deploying applications to a Tomcat instance. It will not make any libraries available for Tomcat directly.

To make Tomcat use any library there are at least these two options:

  1. You can add them as a dependency in war packaged project. So, you might have something wrong in your pom.xml if you already deploy a WAR and still the library does not seem to be found by Tomcat.

  2. As suggested in other answer you can manually copy the dependencies to the tomcat/lib directory and make the dependency in POM <scope>provided</scope> to prevent those to be packaged to the WAR.

The latter option might be sensible if you do not want to deploy monolithic – say 100MB – war every 5 minutes when developing especialy if doing it over network.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement