简体   繁体   中英

Java servlet java.lang.NoClassDefFoundError: org/json/JSONObject

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

最好添加一行以将JSON jar复制到应用程序的WEB-INF / lib而不是Tomcat lib,因为您的应用程序使用它,而不是tomcat本身

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM