尝试运行Tomcat时获取异常

时间:2018-05-16 07:16:02

标签: java maven spring-mvc tomcat servlets

我在IntelliJ中编写了一个Spring MVC应用程序。这是 pom.xml

    <?xml version="1.0" encoding="UTF-8"?>

    <project xmlns="http://maven.apache.org/POM/4.0.0" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
    http://maven.apache.org/xsd/maven-4.0.0.xsd">

      <modelVersion>4.0.0</modelVersion>

      <groupId>com.packt</groupId>
      <artifactId>webstore</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <packaging>war</packaging>

      <name>webstore Maven Webapp</name>
      <!-- FIXME change it to the project's website -->
      <url>http://www.example.com</url>

      <properties>
          <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
          <maven.compiler.source>1.7</maven.compiler.source>
          <maven.compiler.target>1.7</maven.compiler.target>
      </properties>

      <dependencies>
          <dependency>
              <groupId>junit</groupId>
              <artifactId>junit</artifactId>
              <version>4.11</version>
              <scope>test</scope>
         </dependency>
         <dependency>
              <groupId>org.springframework</groupId>
              <artifactId>spring-webmvc</artifactId>
              <version>5.0.6.RELEASE</version>
         </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>5.0.6.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.0.6.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>5.0.6.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>4.0.1</version>
    </dependency>
  </dependencies>

  <build>
    <finalName>webstore</finalName>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.7.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.20.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>3.2.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

当我尝试使用这个pom.xml时,我得到了这个异常。

16-May-2018 12:04:00.662 SEVERE [main] org.apache.catalina.core.ContainerBase.ad
dChildInternal ContainerBase.addChild: start:
 org.apache.catalina.LifecycleException: Failed to initialize component [org.apa
che.catalina.webresources.JarResourceSet@33723e30]
        at org.apache.catalina.util.LifecycleBase.handleSubClassException(Lifecy
cleBase.java:441)
        at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:139)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:173)
        at org.apache.catalina.webresources.StandardRoot.startInternal(StandardR
oot.java:724)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
        at org.apache.catalina.core.StandardContext.resourcesStart(StandardConte
xt.java:4742)
        at org.apache.catalina.core.StandardContext.startInternal(StandardContex
t.java:4877)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
        at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase
.java:742)
        at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:71
8)
        at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:703)

        at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:986)

        at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:
1858)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:51
1)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineEx
ecutorService.java:75)
        at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorS
ervice.java:112)
        at org.apache.catalina.startup.HostConfig.deployWARs(HostConfig.java:772
)
        at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:426
)
        at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1585)
        at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java
:308)
        at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBa
se.java:123)
        at org.apache.catalina.util.LifecycleBase.setStateInternal(LifecycleBase
.java:424)
        at org.apache.catalina.util.LifecycleBase.setState(LifecycleBase.java:36
7)
        at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.ja
va:966)
        at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java
:839)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
        at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.
java:1427)
        at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.
java:1417)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineEx
ecutorService.java:75)
        at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorS
ervice.java:134)
        at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.ja
va:943)
        at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.
java:258)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
        at org.apache.catalina.core.StandardService.startInternal(StandardServic
e.java:422)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
        at org.apache.catalina.core.StandardServer.startInternal(StandardServer.
java:770)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
        at org.apache.catalina.startup.Catalina.start(Catalina.java:682)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:350)
        at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:492)
Caused by: java.lang.IllegalArgumentException: java.util.zip.ZipException: inval
id LOC header (bad signature)
        at org.apache.catalina.webresources.AbstractSingleArchiveResourceSet.ini
tInternal(AbstractSingleArchiveResourceSet.java:143)
        at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:136)
        ... 44 more
Caused by: java.util.zip.ZipException: invalid LOC header (bad signature)
        at java.util.zip.ZipFile.read(Native Method)
        at java.util.zip.ZipFile.access$1400(ZipFile.java:60)
        at java.util.zip.ZipFile$ZipFileInputStream.read(ZipFile.java:734)
        at java.util.zip.ZipFile$ZipFileInflaterInputStream.fill(ZipFile.java:43
4)
        at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:158)
        at sun.misc.IOUtils.readFully(IOUtils.java:65)
        at java.util.jar.JarFile.getBytes(JarFile.java:425)
        at java.util.jar.JarFile.getManifestFromReference(JarFile.java:193)
        at java.util.jar.JarFile.getManifest(JarFile.java:180)
        at org.apache.catalina.webresources.AbstractSingleArchiveResourceSet.ini
tInternal(AbstractSingleArchiveResourceSet.java:141)
        ... 45 more

16-May-2018 12:04:00.680 SEVERE [main] org.apache.catalina.startup.HostConfig.de
ployWAR Error deploying web application archive [C:\Users\Dell\Desktop\TomcatNew
\webapps\webstore.war]
 java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.cata
lina.LifecycleException: Failed to initialize component [org.apache.catalina.web
resources.JarResourceSet@33723e30]
        at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase
.java:746)
        at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:71
8)
        at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:703)

        at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:986)

        at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:
1858)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:51
1)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineEx
ecutorService.java:75)
        at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorS
ervice.java:112)
        at org.apache.catalina.startup.HostConfig.deployWARs(HostConfig.java:772
)
        at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:426
)
        at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1585)
        at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java
:308)
        at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBa
se.java:123)
        at org.apache.catalina.util.LifecycleBase.setStateInternal(LifecycleBase
.java:424)
        at org.apache.catalina.util.LifecycleBase.setState(LifecycleBase.java:36
7)
        at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.ja
va:966)
        at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java
:839)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
        at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.
java:1427)
        at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.
java:1417)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineEx
ecutorService.java:75)
        at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorS
ervice.java:134)
        at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.ja
va:943)
        at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.
java:258)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
        at org.apache.catalina.core.StandardService.startInternal(StandardServic
e.java:422)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
        at org.apache.catalina.core.StandardServer.startInternal(StandardServer.
java:770)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
        at org.apache.catalina.startup.Catalina.start(Catalina.java:682)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:350)
        at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:492)

我搜索了在Stackoverflow上解决此问题,并且在某些帖子中建议将所有依赖项中 pom.xml 的范围更改为提供,但junit one。

我改变了pom后, LifeCycleException 消失了。但是,现在每当我尝试点击 DispatcherServlet 对应的端点时,我都会遇到 HTTP 500错误enter image description here

我查看了这两篇帖子 - 12。我发现由于我的新POM, WEB-INF / lib 文件夹没有JAR / Maven依赖项,导致出现这个新错误。由于这个原因,我陷入僵局。我删除了一个错误和其他表面。

此外,我尝试在 web.xml 中添加 load-on-startup 标记,这又导致 HTTP 404

的web.xml

<web-app>
  <display-name>Archetype Created Web Application</display-name>

  <servlet>
    <servlet-name>DefaultServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>DefaultServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>  

DefaultServlet-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/cache"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <mvc:annotation-driven />
    <context:component-scan base-package="com.packt.webstore" />

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>  

控制器

package com.packt.webstore;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HomeController {


    @RequestMapping("/")
    public String welcome(Model model) {

        model.addAttribute("greeting", "Hey!");
        model.addAttribute("tagline", "Hello!");

        return "welcome";
    }

}   

welcome.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html;
charset=ISO-8859-1">
    <link rel="stylesheet" href="//netdna.bootstrapcdn.com/
bootstrap/3.0.0/css/bootstrap.min.css">
    <title>Welcome</title>
</head>
<body>
<section>
    <div class="jumbotron">
        <div class="container">
            <h1> ${greeting} </h1>
            <p> ${tagline} </p>
        </div>
    </div>
</section>
</body>
</html>

如果有人可以提供帮助,那将会很棒。谢谢。

1 个答案:

答案 0 :(得分:0)

之前我遇到过这个问题,问题原来只是本地存储库中的一些损坏的jar,尝试删除.m2文件夹。

由于您使用的是Windows,因此应该是.m2 repo的路径。

C:\Users\[username]\.m2\repository