java.lang.ClassNotFoundException:net.sf.json.JSONObject

时间:2016-09-20 14:06:48

标签: java json servlets

我试图在我的servlet中初始化JSONObject

        JSONObject obj = new JSONObject();
    UserInfoBean ui= getInfo(empId);

    try{
        obj.put("ID", empId);
        obj.put("Name", empName);
        obj.put("Address", address);
        obj.put("Phone", phone);

    }
    catch(Exception e){
        e.printStackTrace();
    }
}**

我在我的项目中添加了json-lib-2.4-jdk15.jar。右键单击项目 - > BuildPath - >配置构建路径 - >图书馆 - >添加外部Jar。然后添加该特定jar。

但我仍然得到这个错误:

  

SEVERE:Servlet的Servlet.service()   [com.thirdAttempt.UserInformation]在路径[/ ThirdTry]的上下文中   因根本原因引发异常[Servlet执行抛出异常]   java.lang.ClassNotFoundException:net.sf.json.JSONObject at   org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1702)     在   org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1547)     at com.thirdAttempt.UserInformation.doPost(UserInformation.java:48)     在javax.servlet.http.HttpServlet.service(HttpServlet.java:647)at   javax.servlet.http.HttpServlet.service(HttpServlet.java:728)at at   org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)     在   org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)     在   org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)     在   org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)     在   org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)     在   org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)     在   org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)     在   org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)     在   org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)     在   org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)     在   org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)     在   org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)     在   org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)     在   org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)     在   org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:603)     在   org.apache.tomcat.util.net.JIoEndpoint $ SocketProcessor.run(JIoEndpoint.java:312)     在   java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)     在   java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:617)     在java.lang.Thread.run(Thread.java:745)

1 个答案:

答案 0 :(得分:1)

该类找到编译时但不是运行时。我建议您使用maven或gradle进行依赖关系管理,这样您就可以在类路径中指定所需的jar。

<dependency>
    <groupId>net.sf.json-lib</groupId>
    <artifactId>json-lib</artifactId>
    <version>2.4</version>
</dependency>