0

I have a very simple servlet,when I add the JSON library and try to run it i have java.lang.NoClassDefFoundError: org/json/JSONObject (without the json library every thing works fine). I am compiling like this (in ubuntu) :

javac  -classpath ":./libs/json-20160810.jar:/usr/local/tomcat/lib/servlet-api.jar:." *.java && mv *.class /usr/local/tomcat/webapps/ROOT/WEB-INF/classes/

It compiles without any errors, it occurs at runtime.

I am not using any IDE for this.

The servlet is running in a docker:

the docker file :

FROM tomcat:7

RUN apt-get update
RUN apt-get install default-jdk --yes

COPY . /src

WORKDIR /src/

RUN mkdir -p /usr/local/tomcat/webapps/ROOT/WEB-INF/classes

RUN javac  -classpath ./libs/json-20160810.jar:/usr/local/tomcat/lib/servlet-api.jar:. *.java && mv *.class /usr/local/tomcat/webapps/ROOT/WEB-INF/classes/

RUN cp web.xml /usr/local/tomcat/webapps/ROOT/WEB-INF
11
  • add this in web.xml, <dependency> <groupId>org.json</groupId> <artifactId>json</artifactId> <version>20080701</version> </dependency> Commented Oct 14, 2016 at 6:10
  • still the same problem @Jens Commented Oct 14, 2016 at 6:10
  • 1
    @SandeepRoy Looks like op do not use maven. Also you can not add a dependency in a web.xml Commented Oct 14, 2016 at 6:15
  • 1
    as @kuhajeyan mentioned, the json...jar is not in your tomcat classpath. Commented Oct 14, 2016 at 6:38
  • 1
    Yes or better build awar which contains the dependencies Commented Oct 14, 2016 at 6:41

2 Answers 2

2

It is better to add a line to copy the JSON jar to your application's WEB-INF/lib instead of Tomcat lib as your application uses it, not tomcat itself

Sign up to request clarification or add additional context in comments.

1 Comment

Solved the problem.@Peter Gelderbloem
1

You need to provide json libraries in classpath when you run the application as well. Easiest way would be put them in your tomcat's lib folder put json-20160810.jar in lib of tomcat

3 Comments

I need to create lib folder ? because i don't see it . @kuhajeyan
@Maximtoyberman there should be 'lib' folder in tomcat installation. or you need to create a one
Putting the jar into tomcat lib is not a good idea. What is if the app is deployed on another server?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.