Tomcat server won't start in Eclipse when running java class. Caused by: java.lang.NoClassDefFoundError: org/json/JSONException

Tomcat server won't start in Eclipse when running java class. Caused by: java.lang.NoClassDefFoundError: org/json/JSONException

在 Eclipse 中尝试 运行 我的 java class 时,Tomcat 服务器无法启动。关于java.lang.NoClassDefFoundError有几个错误:org/json/JSONException和java.lang.ClassNotFoundException:org.json.JSONException。我在我的项目中导入了 org.json.JSONException,并在我的项目构建路径中添加了 org.json.jar 作为外部 jar。 Org.json.jar 也包含在我的 CLASSPATH 环境变量中。非常感谢任何建议。

尝试启动服务器时 Eclipse 中的控制台错误指出:

SEVERE: A child container failed during start
java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/URL6]]
    at java.util.concurrent.FutureTask.report(Unknown Source)
    at java.util.concurrent.FutureTask.get(Unknown Source)
    at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:931)
    at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:841)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1412)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1402)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/URL6]]
    at org.apache.catalina.util.LifecycleBase.handleSubClassException(LifecycleBase.java:440)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:198)
    ... 6 more
Caused by: java.lang.NoClassDefFoundError: org/json/JSONException
    at java.lang.Class.getDeclaredFields0(Native Method)
    at java.lang.Class.privateGetDeclaredFields(Unknown Source)
    at java.lang.Class.getDeclaredFields(Unknown Source)
    at org.apache.catalina.util.Introspection.getDeclaredFields(Introspection.java:110)
    at org.apache.catalina.startup.WebAnnotationSet.loadFieldsAnnotation(WebAnnotationSet.java:269)
    at org.apache.catalina.startup.WebAnnotationSet.loadApplicationServletAnnotations(WebAnnotationSet.java:137)
    at org.apache.catalina.startup.WebAnnotationSet.loadApplicationAnnotations(WebAnnotationSet.java:69)
    at org.apache.catalina.startup.ContextConfig.applicationAnnotationsConfig(ContextConfig.java:328)
    at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:779)
    at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:299)
    at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:123)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5063)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
    ... 6 more
Caused by: java.lang.ClassNotFoundException: org.json.JSONException
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1358)
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1180)
    ... 19 more

Project Build Configuration

CLASSPATH

打开项目的部署程序集 属性 页面并确保将 jar 设置为作为应用程序的一部分进行部署。 CLASSPATH 环境变量不会影响 Tomcat,一般情况下您应该停止依赖它。

对我来说,有用的是这个...

在servlet程序中,我省略了注释声明中的正斜杠“/”


@WebServlet("/MyServlet") //正确✔️

public class MyServlet 扩展了 HttpServlet{ . . .}


@WebServlet("MyServlet") //错误❌

public class MyServlet 扩展了 HttpServlet{ . . .}