Tomcat7-maven-plugin tomcat7: run java.lang.LinkageError, a previously started download for another type named

Windows Server 2008 R2, 64 Apache Maven 2.2.1 Java Version: 1.6.0_26 JAVA_HOME: C: \ Program Files \ Java \ jdk1.6.0_26 Tomcat 7.0 Compiling a Project with Java 1.6

I am trying to use tomcat7-maven-plugin to start the tomcat dev server using the tomcat7: run target. When I try to click index.jsp for the server, I get:

HTTP Status 500 - java.lang.LinkageError: loader constraint violation: loader (instance of org/apache/jasper/servlet/JasperLoader) previously initiated loading for a different type with name "javax/servlet/http/HttpServletRequest" type Exception report message java.lang.LinkageError: loader constraint violation: loader (instance of org/apache/jasper/servlet/JasperLoader) previously initiated loading for a different type with name "javax/servlet/http/HttpServletRequest" description The server encountered an internal error (java.lang.LinkageError: loader constraint violation: loader (instance of org/apache/jasper/servlet/JasperLoader) previously initiated loading for a different type with name "javax/servlet/http/HttpServletRequest") that prevented it from fulfilling this request. exception javax.servlet.ServletException: java.lang.LinkageError: loader constraint violation: loader (instance of org/apache/jasper/servlet/JasperLoader) previously initiated loading for a different type with name "javax/servlet/http/HttpServletRequest" org.apache.jasper.servlet.JspServlet.service(JspServlet.java:343) javax.servlet.http.HttpServlet.service(HttpServlet.java:722) root cause java.lang.LinkageError: loader constraint violation: loader (instance of org/apache/jasper/servlet/JasperLoader) previously initiated loading for a different type with name "javax/servlet/http/HttpServletRequest" java.lang.Class.getDeclaredMethods0(Native Method) java.lang.Class.privateGetDeclaredMethods(Class.java:2427) java.lang.Class.getDeclaredMethods(Class.java:1791) org.apache.catalina.util.Introspection.getDeclaredMethods(Introspection.java:108) org.apache.jasper.servlet.JspServletWrapper.getServlet(JspServletWrapper.java:172) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:369) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334) javax.servlet.http.HttpServlet.service(HttpServlet.java:722) 

I have successfully used tomcat7: deploy to deploy the same code to a local Tomcat Windows service instance. When I access the local server instance, there are no errors.

My code depends on javax.servlet.http.HttpServlet through this maven dependency:

 <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> </dependency> 

Given the error I am getting, I am sure that there is a class loading conflict for this dependency. I cannot understand how / why / where the conflict is; that is, where banks conflict, and how / why this happens when I try to start using tomcat7: run, but not when I start "standalone" using my local tomcat instance.

POM:

 <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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.moring.gloak</groupId> <artifactId>gloak-registration</artifactId> <packaging>war</packaging> <version>1.0.0-SNAPSHOT</version> <name>gloak-registration Maven Webapp</name> <build> <finalName>gloak-registration</finalName> <plugins> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.0-SNAPSHOT</version> <configuration> <server>local_tomcat</server> <url>http://localhost:9280/manager/text</url> <update>true</update> <port>9280</port> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.5.1</version> <configuration> <source>1.6</source> <target>1.6</target> </configuration> </plugin> </plugins> </build> <repositories> <repository> <id>people.apache.snapshots</id> <url>http://repository.apache.org/content/groups/snapshots-group/</url> <releases> <enabled>false</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> </dependency> </dependencies> </project> 

project web.xml

 <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Archetype Created Web Application</display-name> <servlet> <servlet-name>registrationServlet</servlet-name> <servlet-class>com.moring.gloak.web.register.RegistrationServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>registrationServlet</servlet-name> <url-pattern>/register</url-pattern> </servlet-mapping> </web-app> 

tomcat web.xml webapp expression from maven target dir:

 <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> 

valid servlet code:

 package com.moring.gloak.web.register; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.HashMap; import java.util.Map; public final class RegistrationServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.getRequestDispatcher("index.jsp").forward(request, response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Map<String, String> messages = new HashMap<String, String>(); request.setAttribute("messages", messages); // Get and validate name. String serviceName = request.getParameter("serviceName"); if (serviceName == null || serviceName.trim().isEmpty()) { messages.put("name", "Please enter service name"); } else if (!serviceName.matches("\\p{Alnum}+")) { messages.put("name", "Please enter alphanumeric characters only"); } if (messages.isEmpty()) { messages.put("success", String.format("Service name is %s", serviceName)); } request.getRequestDispatcher("index.jsp").forward(request, response); } } 
+6
source share
2 answers

Biju Kunjummen answered the question in his comment on my original post. Thanks to Biju Kunyummen! Please vote for his comment.

My answer to my question is only a little more detailed.

The servlet-api dependency in pom.xml needs a "provided" scope. This is because Tomcat already provides (requires and uses) the servlet-api dependency. The rules for defining Maven dependency rules are defined here:

http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Dependency_Scope

Corrected servlet-api xml dependency:

 <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> 

Why did the previous xml work when I launched my war on a local tomcat instance and did not run it in process (ie with the goal of tomcat7: run)? I have no exact answer. The maven command on the process server explicitly loads dependencies differently than the local tomcat instance.

My contribution from this is that although I may need to depend on compiling some code, I need to keep in mind that if I deploy this code in some container, I need to use the maven provided scope to make sure that the dependencies do not collide.

+15
source

for me it worked like.

  <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.0.1</version> <scope>provided</scope> </dependency> 

just adding an area depending

0
source

All Articles