如何使用嵌入式Tomcat运行Spring MVC项目?报错求助
Hey there! Let's work through this problem step by step—first we'll squash that annoying error, then we'll get you set up to run your Spring project the right way with embedded Tomcat, so you don't need a standalone installation.
First: Why You're Seeing That Error
The key line in your stack trace that tells us what's wrong is this:
Caused by: java.util.zip.ZipException: invalid LOC header (bad signature)
This means a JAR file somewhere is corrupted—either one of your project's Maven dependencies (most likely) or, less probable since you reinstalled Tomcat, a JAR in your standalone Tomcat setup. Reinstalling Tomcat won't fix this because the corrupted file lives in your Maven local repository, not the standalone Tomcat files.
How to Fix the Corrupted JAR
- Clean your Maven local repository:
- Locate your Maven repo (usually
~/.m2/repositoryon macOS/Linux orC:\Users\<YourUsername>\.m2\repositoryon Windows). - Delete the entire
org/apache/tomcat/embedfolder (since you're using thetomcat-embed-jasperdependency) or, for a full refresh, delete the entire repository folder. - Go back to your project and run
mvn clean installin your terminal/command prompt. This will re-download all your dependencies fresh, replacing any corrupted files.
- Locate your Maven repo (usually
- Double-check the fix: After downloading, try opening one of the Tomcat embed JARs with a zip tool—if it opens without errors, the corruption is gone.
Second: Running Your Project with Embedded Tomcat (No Standalone Tomcat Needed!)
Since you're using Spring and already added the tomcat-embed-jasper dependency, you don't need to install a standalone Tomcat at all. Here's how to run your project correctly:
If You're Using Spring Boot (Most Common for New Spring Projects)
- Make sure your project has a main class annotated with
@SpringBootApplication:@SpringBootApplication public class YourSpringApp { public static void main(String[] args) { SpringApplication.run(YourSpringApp.class, args); } } - Just run this main class directly from your IDE (like IntelliJ or Eclipse) or use the Maven command
mvn spring-boot:run—this will start the embedded Tomcat server automatically. You can access your JSPs athttp://localhost:8080/your-app-context.
If You're Using a Traditional Spring Web Project
Add the Tomcat Maven plugin to your pom.xml:
<build> <plugins> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <configuration> <port>8080</port> <path>/</path> <!-- Set your app's context path here --> </configuration> </plugin> </plugins> </build>
Then run mvn tomcat7:run in your project directory—this will spin up an embedded Tomcat instance and deploy your project to it instantly.
Why Your Original Approach Caused Issues
When you installed a standalone Tomcat and deployed your project to it, you were mixing two separate Tomcat environments: the embedded one from your Maven dependencies and the standalone one. This can lead to classpath conflicts, but in your case, the immediate blocker was the corrupted JAR file.
内容的提问来源于stack exchange,提问作者Karen Goh

