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

时间:2022-07-01 19:41:19

Am trying to initialize JSONObject in my servlet

我试图在我的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();
    }
}**

I have added json-lib-2.4-jdk15.jar in my project. Right Click on the project -> BuildPath -> Configure Build Path -> Libraries -> Add External Jar .Then added that particular jar.

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

But still I get this error:

但我仍然得到这个错误:

SEVERE: Servlet.service() for servlet [com.thirdAttempt.UserInformation] in context with path [/ThirdTry] threw exception [Servlet execution threw an exception] with root cause java.lang.ClassNotFoundException: net.sf.json.JSONObject at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1702) at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1547) at com.thirdAttempt.UserInformation.doPost(UserInformation.java:48) at javax.servlet.http.HttpServlet.service(HttpServlet.java:647) at javax.servlet.http.HttpServlet.service(HttpServlet.java:728) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603) at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at java.lang.Thread.run(Thread.java:745)

严重:servlet [com.thirdAttempt.UserInformation]的Servlet.service()在路径[/ 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)在com.thirdAttempt.UserInformation.doPost(UserInformation.java: 48)在javax.servlet.http.HttpServlet.service(HttpServlet.java:647)在javax.servlet.http.HttpServlet.service(HttpServlet.java:728)在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain .java:305)在org.apache的org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)。 org.apache.catalina.core上的catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) .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)at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:617)at java.lang.Thread.run( Thread.java:745)

1 个解决方案

#1


1  

The class is found compile time but not runtime. I suggest you use maven or gradle for dependency management, that way you can specify what jars you want in your classpath like this.

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

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

#1


1  

The class is found compile time but not runtime. I suggest you use maven or gradle for dependency management, that way you can specify what jars you want in your classpath like this.

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

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