java.lang.NoClassDefFoundError:javax / el / ELManager

时间:2017-08-23 13:42:49

标签: java maven tomcat noclassdeffounderror

我正在使用Spring Tool Suite在Spring上使用webapp。如果我使用IDE在所提供的Pivotal tc服务器上构建和部署应用程序,它就可以正常工作。但是,如果我做一个手册" mvn clean package"构建并尝试将其部署到独立的Tomcat服务器(使用最新的Tomcat 7),它会引发以下异常:

2017-08-23 15:24:13 WARN  AnnotationConfigWebApplicationContext:551 - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerAdapter' defined in org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter]: Factory method 'requestMappingHandlerAdapter' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mvcValidator' defined in org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: javax/el/ELManager
2017-08-23 15:24:13 ERROR DispatcherServlet:502 - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerAdapter' defined in org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter]: Factory method 'requestMappingHandlerAdapter' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mvcValidator' defined in org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: javax/el/ELManager

经过进一步检查,它确实在没有加载罐子的情况下抱怨几条线:

sie 23, 2017 3:24:12 PM org.apache.catalina.startup.HostConfig deployWAR
INFO: Deploying web application archive D:\Apache-Tomcat-7.0\webapps\TestApp-0.0.1-SNAPSHOT.war
sie 23, 2017 3:24:12 PM org.apache.catalina.loader.WebappClassLoader validateJarFile
INFO: validateJarFile(D:\Apache-Tomcat-7.0\webapps\TestApp-0.0.1-SNAPSHOT\WEB-INF\lib\el-api-2.2.jar) - jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending class: javax/el/Expression.class
sie 23, 2017 3:24:12 PM org.apache.catalina.loader.WebappClassLoader validateJarFile
INFO: validateJarFile(D:\Apache-Tomcat-7.0\webapps\TestApp-0.0.1-SNAPSHOT\WEB-INF\lib\javax.servlet-api-3.1.0.jar) - jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending class: javax/servlet/Servlet.class
sie 23, 2017 3:24:12 PM org.apache.catalina.loader.WebappClassLoader validateJarFile
INFO: validateJarFile(D:\Apache-Tomcat-7.0\webapps\TestApp-0.0.1-SNAPSHOT\WEB-INF\lib\tomcat-el-api-8.0.21.jar) - jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending class: javax/el/Expression.class
2017-08-23 15:24:13 INFO  ContextLoader:304 - Root WebApplicationContext: initialization started

我的pom.xml:

<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.exmaple.mvc</groupId>
  <artifactId>TestApp</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>4.3.10.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.3.10.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>4.3.10.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>4.3.10.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.6.6</version>
    </dependency>
    <dependency>
       <groupId>log4j</groupId>
       <artifactId>log4j</artifactId>
       <version>1.2.17</version>
       <exclusions>
        <exclusion>
            <artifactId>jms</artifactId>
            <groupId>javax.jms</groupId>
        </exclusion>
        <exclusion>
            <artifactId>jmxri</artifactId>
            <groupId>com.sun.jmx</groupId>
        </exclusion>
        <exclusion>
            <artifactId>jmxtools</artifactId>
            <groupId>com.sun.jdmk</groupId>
            </exclusion>
        </exclusions>
    </dependency>
        <dependency>
        <groupId>jstl</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.mockito/mockito-all -->
    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-all</artifactId>
        <version>1.9.5</version>
        <scope>test</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/commons-lang/commons-lang -->
    <dependency>
        <groupId>commons-lang</groupId>
        <artifactId>commons-lang</artifactId>
        <version>2.3</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-validator -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>6.0.1.Final</version>
    </dependency>

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

    <build>
        <plugins>
            <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
        </plugins>
    </build>

</project>

这种行为的原因是什么?如何解决这个问题?

编辑更多信息:

<scope>provided</scope>添加到javax.servlet-api似乎可以解决有关javax.servlet-api未在开始时加载的警告。问题el-api仍然存在。

我已经检查了tomcat / lib目录,其中已经包含el-api.jar,这可能就是为什么它告诉我它不会加载我在pom.xml中列出的那个。问题是,添加<scope>provided</scope>也无法解决问题。无论我做什么,它仍然抱怨给我同样的java.lang.NoClassDefFoundError: javax/el/ELManager错误。

除了上面关于javax.servlet-api的编辑中的部分之外,el-api的问题在于我在版本2.2中运行了带有el-api jar的Tomcat 7。缺少的类是在el-api 3.0中引入的。在Tomcat 8中运行相同的webapp(使用el-api 3.0 jar)可以正常运行。

8 个答案:

答案 0 :(得分:21)

你错过了javax.el-api作为依赖。添加:

<dependency>
    <groupId>javax.el</groupId>
    <artifactId>javax.el-api</artifactId>
    <version>3.0.0</version>
</dependency>

到你的pom.xml

答案 1 :(得分:2)

请参阅Hibernate validation "Unable to initialize javax.el.ExpressionFactory" error

使用

<dependency>
   <groupId>org.glassfish</groupId>
   <artifactId>javax.el</artifactId>
   <version>3.0.1-b08</version>
</dependency>

答案 2 :(得分:2)

为那些拥有 Tomcat 7 并尝试应用所选答案的人添加我的答案。

如果添加依赖项对您没有帮助,而您继续获得

java.lang.NoClassDefFoundError: javax/el/ELManager

那是因为 Tomcat 7 不允许您使用 javax.* 包中的对象加载更现代的 jar。它将打印到日志:

<块引用>

jakarta.el-3.0.3.jar - jar 未加载。请参阅 Servlet 规范 3.0,第 10.7.2 节。违规类:javax/el/Expression.class

解决方案是升级您的 Tomcat 或将 hibernate-validator 降级到最新的 5.x 版本。另一个(我的 POV 不太喜欢)选项是尝试替换 tomcat7/lib 文件夹中的 jar。

答案 3 :(得分:1)

就我而言, 注释掉这种依赖,

<!-- <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>6.0.8.Final</version>
    </dependency> -->

并添加了

    <dependency>
        <groupId>javax.el</groupId>
        <artifactId>javax.el-api</artifactId>
        <version>3.0.0</version>
        <scope>provided</scope>
    </dependency> 

这解决了我的问题

答案 4 :(得分:1)

将hibernate-validator降级至版本5,它将在Tomcat 7上正常运行。 就我而言,我将下一个依赖项添加到pom中:

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>5.4.3.Final</version>
</dependency>

或者您可以将新的el-api.jat添加到tomcat lib文件夹中。

答案 5 :(得分:0)

尝试以下操作:

  • 像其他mention一样将javax.eljavax.el-api添加到pom.xml中
  • 如果您具有不同的类加载器(在使用OSGi时通常如此),则需要将上下文临时设置为保存实现的上下文加载器。将您的电话换成:

    ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader();
    try {
      Class<?> currentClass = this.getClass(); // or any class that is in a bundle with the dependency 
      Thread.currentThread().setContextClassLoader(currentClass.getClassLoader());
      // execute library call
    } finally {
      Thread.currentThread().setContextClassLoader(originalClassLoader); // back to original context
    }
    
  • 向META-INF / services /

  • 添加服务
  • 将属性添加到$ java.home / lib / el.properties
  • 将系统属性与factoryId一起使用

当库通过> ExpressionFactory> ELManager.newInstance(..)实例化FactoryFinder.find(..)时,它有几种策略来查找实现。呼叫是这样硬编码的:

public static ExpressionFactory newInstance(Properties properties) {
    return (ExpressionFactory) FactoryFinder.find(
        "javax.el.ExpressionFactory", "com.sun.el.ExpressionFactoryImpl", properties);
}

有关更多信息,请参见javax.el.FactoryFinder.find(..)的来源

答案 6 :(得分:0)

我正在使用Tomcat 7.0.9,由于公司的官僚作风,无法更改为任何较新的版本。将EL导入为依赖项也无法解决。

在Tomcat的根文件夹-> lib中,我用新的3.0(来自本地<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button id="box">Button</button> <p></p>)替换了旧的el-api(2.2版本)。然后,正确隔离tomcat的依赖关系(如here所示)使我的WAR正确部署在Tomcat 7.0.9上。

答案 7 :(得分:0)

Hibernate Validator 6.x-> Bean验证2.0(JSR 380)-> EL3.0

Hibernate Validator 5.x-> Bean Validation 1.1(JSR 349)-> EL2.2

Bean Validation 1.0(JSR 303)->(我不确定)

因此,它也会影响其他版本(tomcat,jdk,jsp,servlet)

例如tomcat7,如果要使用Hibernate Validator,则应使用Hibernate Validator 5.x,el 2.2(以及Servlet 3.0,jsp 2.2和jdk 6 +)