Liferay 6.1+Tomcat7.0环境下javax.servlet-api-3.0.1.jar无法加载问题排查
Hey, I’ve run into this exact issue with Liferay portlets before—let’s break down what’s causing the error and how to fix it quickly.
为什么会出现这个报错?
The error message points to a key rule from the Servlet Spec 2.3, Section 9.7.2: your web application is trying to include a jar file that contains core Servlet API classes, which are already provided by the Tomcat server bundled with Liferay.
Here’s the breakdown:
- Liferay 6.1.1 CE GA2 uses Tomcat 7.0.27, which already includes its own copy of the Servlet API (matching Servlet 3.0 spec).
- Your custom
SiteSkills_CMS-portlethas packagedjavax.servlet-api-3.0.1.jarinside itsWEB-INF/libdirectory. This jar contains thejavax.servlet.Servlet.class—a class that Tomcat already manages. - To avoid class loading conflicts, Tomcat refuses to load your duplicate jar, which prevents the entire web app from starting up.
怎么解决这个问题?
There are two straightforward fixes, depending on how you build your portlet:
1. Use "provided" scope for Servlet API dependency (Recommended)
If you’re using a build tool like Maven or Gradle, mark the Servlet API dependency as provided. This tells the build tool not to package the jar into your portlet’s WEB-INF/lib folder, since the server (Tomcat/Liferay) will provide it at runtime.
Maven example:
<dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.0.1</version> <scope>provided</scope> </dependency>
Gradle example:
providedCompile 'javax.servlet:javax.servlet-api:3.0.1'
2. Manually remove the conflicting jar
If you’re not using a build tool, simply:
- Locate your portlet’s
WEB-INF/libdirectory (in the temp folder mentioned, or your packaged WAR file) - Delete the
javax.servlet-api-3.0.1.jarfile - Redeploy the portlet to Liferay
After making either change, restart your Liferay server (or just redeploy the portlet) and the error should disappear.
内容的提问来源于stack exchange,提问作者rahul

