Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot with non-embedded Tomcat: loader constraint violation error

I have an error when trying to display a JSP with Spring Boot. Here is my configuration:

  • Spring source STS (last version)
  • Spring Boot 1.1.8 (last release to date)
  • Java 1.7
  • Tomcat 8.0.14 (I'm not using the embedded tomcat, so I launch manually with the spring boot application added)

My JSP file is barebones:

TEST

Here is my 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.edhec</groupId>
<artifactId>stdapps</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>

<name>stdapps</name>
<description>Project stdapps</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.1.8.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-rest</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-websocket</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-c3p0</artifactId>
        <version>4.3.6.Final</version>
    </dependency>
    <dependency>
        <groupId>com.oracle</groupId>
        <artifactId>ojdbc14</artifactId>
        <version>10.2.0.4.0</version>
    </dependency>
    <!-- CAS Client -->
    <dependency>
       <groupId>org.jasig.cas.client</groupId>
       <artifactId>cas-client-core</artifactId>
       <version>3.3.3</version>
       <scope>runtime</scope>
       <type>jar</type>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-cas</artifactId>
        <type>jar</type>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-taglibs</artifactId>
    </dependency>
    <dependency>
        <groupId>commons-beanutils</groupId>
        <artifactId>commons-beanutils</artifactId>
        <version>1.9.2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.3.2</version>
    </dependency>

</dependencies>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <start-class>com.edhec.stdapps.Application</start-class>
    <java.version>1.7</java.version>
    <tomcat.version>8.0.14</tomcat.version>
</properties>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

Here is the error I get :

java.lang.LinkageError: loader constraint violation: when resolving method "org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(Ljavax/servlet/ServletConfig;)Lorg/apache/tomcat/InstanceManager;" the class loader (instance of org/apache/jasper/servlet/JasperLoader) of the current class, org/apache/jsp/WEB_002dINF/views/agreement2/index_jsp, and the class loader (instance of java/net/URLClassLoader) for resolved class, org/apache/jasper/runtime/InstanceManagerFactory, have different Class objects for the type .getInstanceManager(Ljavax/servlet/ServletConfig;)Lorg/apache/tomcat/InstanceManager; used in the signature

I think it has a relation with el or jsp-api version, I try to add manually in the pom.xml but got same error or sometimes 'noClassDefFoundError on my compiled jsp'

Does someone see what's going wrong?

like image 477
partyboy Avatar asked Dec 01 '22 15:12

partyboy


1 Answers

Due to your dependency on spring-boot-starter-web and spring-boot-starter-websocket and their transitive dependencies, you're packaging Tomcat inside your war file. This is leading to there being two different versions of Tomcat's classes available and is caused the constraint violation. You need to mark the Tomcat dependencies as provided so that they don't get packaged in WEB-INF/lib.

Add the following dependencies to your pom:

<dependency> 
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-websocket</artifactId>
    <scope>provided</scope>
</dependency>

It's preferable to mark the dependencies as provided, rather than excluding them altogether, as it allows your war file to be deployed to Tomcat or executed with java -jar. This works because the provided dependencies are packaged in WEB-INF/lib-provided where they'll be ignored by a standalone Servlet container but be available when you're using the embedded container.

like image 128
Andy Wilkinson Avatar answered Dec 06 '22 16:12

Andy Wilkinson