0

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.

parsecer
  • 4,758
  • 13
  • 71
  • 140
  • 1
    Try put this jar in your tomcat `lib` folder. – Kamil W Oct 29 '18 at 19:24
  • in your .m2/settings.xml, did you configure your localRepository? – wenzi Oct 29 '18 at 19:39
  • @wenzi No, I don't think so. All I did was to run a programm to install Maven insetlf and that's about it. – parsecer Oct 29 '18 at 19:44
  • https://maven.apache.org/settings.html, maybe you can try the section "Simple Values" to let it pull from your local repository – wenzi Oct 29 '18 at 19:52
  • 1
    if you use `war` in your pom file, it will package all your dependencies in the war, then you could just deploy that war file to your tomcat environment. – Brandon G Oct 29 '18 at 19:59

2 Answers2

1

If you put your .jar file into your tomcat folder lib, it will work like a charm!

Jonathan Gagne
  • 4,241
  • 5
  • 18
  • 30
  • Thank you, it did work. However, I can't do it with every single jar out there manually, can I? Is there a way to keep the jars in .m2 and let tomcat know of it? – parsecer Oct 29 '18 at 19:47
1

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.

pirho
  • 11,565
  • 12
  • 43
  • 70