• IDEA启动tomcat报错:java.lang.NoClassDefFoundError: org/springframework/context/ApplicationContext、ContainerBase.addChild: start: org.apache.catalina.LifecycleException: Failed to start component


      先看错误日志:

    27-May-2019 17:07:29.838 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log Server version:        Apache Tomcat/9.0.0.M26
    27-May-2019 17:07:29.844 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log Server built:          Aug 2 2017 20:29:05 UTC
    27-May-2019 17:07:29.845 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log Server number:         9.0.0.0
    27-May-2019 17:07:29.845 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log OS Name:               Windows 10
    27-May-2019 17:07:29.845 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log OS Version:            10.0
    27-May-2019 17:07:29.845 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log Architecture:          amd64
    27-May-2019 17:07:29.845 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log Java Home:             D:DevJavajdk1.8.0_102jre
    27-May-2019 17:07:29.845 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Version:           1.8.0_102-b14
    27-May-2019 17:07:29.845 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Vendor:            Oracle Corporation
    27-May-2019 17:07:29.845 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_BASE:         C:Userswulf.IntelliJIdea2018.3system	omcatUnnamed_manage_system_home_2
    27-May-2019 17:07:29.845 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_HOME:         D:Devapache-tomcat-9.0.0.M26
    27-May-2019 17:07:29.846 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.config.file=C:Userswulf.IntelliJIdea2018.3system	omcatUnnamed_manage_system_home_2conflogging.properties
    27-May-2019 17:07:29.847 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
    27-May-2019 17:07:29.848 信息 [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:51337,suspend=y,server=n
    Caused by: java.lang.NoClassDefFoundError: org/springframework/context/ApplicationContext
        at java.lang.Class.getDeclaredFields0(Native Method)
        at java.lang.Class.privateGetDeclaredFields(Class.java:2583)
        at java.lang.Class.getDeclaredFields(Class.java:1916)
        at org.apache.catalina.util.Introspection.getDeclaredFields(Introspection.java:110)
        at org.apache.catalina.startup.WebAnnotationSet.loadFieldsAnnotation(WebAnnotationSet.java:262)
        at org.apache.catalina.startup.WebAnnotationSet.loadApplicationListenerAnnotations(WebAnnotationSet.java:88)
        at org.apache.catalina.startup.WebAnnotationSet.loadApplicationAnnotations(WebAnnotationSet.java:64)
        at org.apache.catalina.startup.ContextConfig.applicationAnnotationsConfig(ContextConfig.java:328)
        at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:778)
        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:5003)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
        ... 44 more
    Caused by: java.lang.ClassNotFoundException: org.springframework.context.ApplicationContext
        at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1269)
        at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1104)
        ... 57 more
    
    27-May-2019 17:07:36.530 严重 [RMI TCP Connection(3)-127.0.0.1] org.apache.tomcat.util.modeler.BaseModelMBean.invoke Exception invoking method manageApp
     java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/wlf-voicebox]]
        at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:744)
        at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:716)
        at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:703)
        at org.apache.catalina.startup.HostConfig.manageApp(HostConfig.java:1729)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:287)
        at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
        at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
        at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:456)
        at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:405)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:287)
        at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
        at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
        at com.sun.jmx.remote.security.MBeanServerAccessController.invoke(MBeanServerAccessController.java:468)
        at javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1468)
        at javax.management.remote.rmi.RMIConnectionImpl.access$300(RMIConnectionImpl.java:76)
        at javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1309)
        at java.security.AccessController.doPrivileged(Native Method)
        at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1408)
        at javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:829)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:324)
        at sun.rmi.transport.Transport$1.run(Transport.java:200)
        at sun.rmi.transport.Transport$1.run(Transport.java:197)
        at java.security.AccessController.doPrivileged(Native Method)
        at sun.rmi.transport.Transport.serviceCall(Transport.java:196)
        at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:568)
        at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:826)
        at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:683)
        at java.security.AccessController.doPrivileged(Native Method)
        at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:682)
        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)
    
    27-May-2019 17:07:36.532 严重 [RMI TCP Connection(3)-127.0.0.1] org.apache.tomcat.util.modeler.BaseModelMBean.invoke Exception invoking method createStandardContext
     javax.management.RuntimeOperationsException: Exception invoking method manageApp
        at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:295)
        at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
        at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
        at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:456)
        at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:405)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:287)
        at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
        at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
        at com.sun.jmx.remote.security.MBeanServerAccessController.invoke(MBeanServerAccessController.java:468)
        at javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1468)
        at javax.management.remote.rmi.RMIConnectionImpl.access$300(RMIConnectionImpl.java:76)
        at javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1309)
        at java.security.AccessController.doPrivileged(Native Method)
        at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1408)
        at javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:829)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:324)
        at sun.rmi.transport.Transport$1.run(Transport.java:200)
        at sun.rmi.transport.Transport$1.run(Transport.java:197)
        at java.security.AccessController.doPrivileged(Native Method)
        at sun.rmi.transport.Transport.serviceCall(Transport.java:196)
        at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:568)
        at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:826)
        at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:683)
        at java.security.AccessController.doPrivileged(Native Method)
        at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:682)
        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)
    Caused by: java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/wlf-voicebox]]
        at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:744)
        at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:716)
        at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:703)
        at org.apache.catalina.startup.HostConfig.manageApp(HostConfig.java:1729)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:287)
        ... 35 more
    
    27-May-2019 17:07:41.469 信息 [ContainerBackgroundProcessor[StandardEngine[Catalina]]] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [D:Devapache-tomcat-9.0.0.M26webappsmanager]

      日志里告诉我们tomcat启动时加载类失败了,导致web容器无法启动。定位问题首先得场景复现,这里我从头开始进行。一开始我的tomcat是可以启动的,但一修改了pom文件后就出现了上述异常,所以我从正常的设置开始。Eclipse与IDEA的一个不同点是tomcat的部署:Eclipse默认支持代码改动后重启tomcat;IDEA需要先自己打出个war包,再自己去重启tomcat。为了避免每次都打包耗费大量时间(虽然每次打包时间不多,但你若频繁变动代码,积少成多,聚沙成塔,每天将耗费不少时间在打包上)。我们需要设置IDEA打war exploded包,它跟war有啥区别呢?

      war包:Web application ARchive,其实也是一种JAR文件,自然也是一种压缩包,包含用来JSP、Java Servlet、Java类、XML文件、标签库、静态网页(HTML和相关文件),以及构成Web应用程序的其他资源;

      exploded包:其实就是war包的展开,也就是war、jar等产出物没压缩前的目录结构。在开发的时候使用这种模式可使修改文件的效果立刻显现出来。

      另外我这个项目的jar包都是本地引用,而非引用了maven仓库的jar包,我的pom文件需要制定范围和引用地址:

            <dependency>
                <groupId>javazoom</groupId>
                <artifactId>jlayer</artifactId>
                <version>1.0.1</version>
                <scope>system</scope>
                <systemPath>${project.basedir}/src/main/webapp/lib/jlayer-1.0.1.jar</systemPath>
            </dependency>

      上面可以看到我将jar包放在src/main/webapp/lib这个目录下,而这个目录并非maven的约定目录(这点是关键)。接着我们需要进行部署的配置,通过快捷键Ctrl+Alt+Shift+S弹出项目结构(Project Structure)窗口,左边菜单中的Project主要用来指定JDK,接着我们看下Modules,里面有3个tab

    • Sources:显示项目的目录资源,都是项目部署的时候需要的目录,不同颜色代表不同的类型,选中左边的目录再点击上面Mark as对应的类型即可设置。详见下图;
    • Paths:可以指定项目的编译输出目录,即项目类和测试类的编译输出地址,使用默认路径即可
    • Dependencies:项目的依赖,使用默认依赖即可

      这几类目录都比较好理解,需要注意的是Excluded Folders配置的是让IDEA忽略的目录,一般是编译后的输出目录,这里加入的lib目录是我们的本地jar包目录,我们只是用来配置jar包,部署时会在WEB-INF自动生成对应lib包,所以可以忽略。

      跳过Libraries和Facets,我们直接进入部署的关键点。打开IDEA的artifact -> 点击左边菜单栏的Artifacts -> 选中原有的artifact(默认是war包的artifact)并点减号删除 -> 点加号新增一个artifact -> 下拉框选第二个(Exploded) -> 继续二级下拉框选来自Module(From Modules) -> 选中之前配置的Module即可。

      这时你可以看到默认设置了一个output目录,tomcat启动时读取的就是该目录下的相关文件和资源。Output Layout就是output里面的布局,我们可以看到其实就是WEB-INF目录,它有编译后的字节码文件所在的classes目录和引入的jar包所在的lib目录。

      最后一步就是配置tomcat server,在Deployment中加入我们刚刚配置好的artifact。

      当你点击运行tomcat时,IDEA开始干这些事情:

    • 编译,IDEA在保存/自动保存后不会做编译,不像Eclipse的保存即编译,因此在运行server前会做一次编译(其实就是maven构建,这就是为啥我们可以不用自己用maven跑一遍的原因)。编译后class文件存放在指定的项目编译输出目录(target)下;
    • 根据artifact中的设定对目录结构(如上面的WEB-INF的classes和lib)进行创建;
    • 拷贝web资源的根目录下的所有文件到artifact的目录(不仅仅是WEB-INF的classes和lib)下;
    • 拷贝编译输出目录下的classes目录到artifact下的WEB-INF(WEB-INF/classes)下;
    • 拷贝lib目录下所需的jar包到artifact下的WEB_INF/lib下;
    • 运行server,运行成功后,自动打开浏览器访问指定url

      以上是正常情况,废话不多说,复现异常:把pom文件的一把jar包由本地引用改为maven引入本地仓库,如下:

            <dependency>
                <groupId>javazoom</groupId>
                <artifactId>jlayer</artifactId>
                <version>1.0.1</version>
            </dependency>

      接着按IDEA的提示Import Changes(导入pom文件变动),直接重启tomcat,再看下我们的artifact的WEB-INF/lib目录已经为空了:

      同样的,Project Structure的Artifacts下的lib目录不翼而飞了:

      这里因为jar包引用了maven的本地仓库,导致我们的war结构(其实就是WEB-INF/lib目录)发生了变动,lib目录的jar包被清空了。为啥呢?因为我们通过maven引入jar包,那么就得按maven的规则来玩了。它将自动创建一个war包的artifact,它将从webapp/WEB-INF/lib目录取jar包,而我们没有该目录,那么它会创建一个,但里面只有一个我们配置的jar包(jlayer-1.0.1.jar),然后tomcat启动加载类时bug出现了。既然Spring的jar包都没了,自然tomcat启动时会找不到ApplicationContext这个类了。

      解决办法有两个,一个是将maven引入的那个jar包改用本地引入;另一个是遵守maven的约定,修改lib目录,从webapp/lib挪到webapp/WEB-INF/lib并对应修改pom文件里引入本地jar包的路径。第一个办法还是上面的老路子,修改pom文件,重新设置Artifacts(先删掉现有的所有artifact,因为maven创建了一个默认的war包artifact,再加上原来的war exploded,所以会有两个,然后新增一个新的war exploded),删掉前面maven自动生成的target/工程名/WEB-INF下的lib目录,重启tomcat(参见前面启动tomcat时IDEA干的事,如果不重启tomcat,artifact的输出目录下lib将会一直是空的,它需要tomcat启动时去复制target目录下的lib包到artifact的输出目录下);第二个办法更合理一些,改完后同样需要重新设置Artifacts,删掉target/工程名下的lib目录,重启tomcat。

  • 相关阅读:
    Java EE (3) -- Java EE 6 Web Services Developer Certified Expert(1z0-897)
    二、用电信号传输 TCP/IP 数据(1)
    P2384 最短路 洛谷
    T1231 最优布线 codevs
    P3371 单源最短路径【模板】 洛谷
    spfa【模板】
    P1396 营救 洛谷
    解决Android加固多进程ptrace反调试的思路整理
    Android Dex文件格式解析
    360加固保so动态脱壳
  • 原文地址:https://www.cnblogs.com/wuxun1997/p/10933625.html
Copyright © 2020-2023  润新知