ClassFormatError ServletException Maven项目错误

时间:2017-08-01 16:17:39

标签: java spring maven servlets spring-junit

我有一个maven项目,它有一个测试类:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({
    "file:../test-war/src/main/webapp/WEB-INF/applicationContext.xml",
    "file:../test-war/src/main/webapp/WEB-INF/config/servlet-context.xml"
})
@Transactional
public class SpringTest  {

该项目使用“mvn clean install”运行 引发错误:

java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/servlet/ServletException
  at java.lang.ClassLoader.defineClass1(Native Method)
  at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
  at org.springframework.core.type.StandardAnnotationMetadata.hasAnnotatedMethods(StandardAnnotationMetadata.java:161)

已经尝试过很多东西,但仍然无法工作,有什么想法吗?

这是pom.xml的相关依赖项:

 <dependency>
        <groupId>javaee</groupId>
        <artifactId>javaee-api</artifactId>
    </dependency>
    <dependency>
        <groupId>org.javassist</groupId>
        <artifactId>javassist</artifactId>
        <version>3.20.0-GA</version>
        </dependency>
    <dependency>
        <groupId>org.apache.geronimo.specs</groupId>
        <artifactId>geronimo-servlet_3.0_spec</artifactId>
        <version>1.0</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <scope>test</scope>
    </dependency>

来自父pom.xml的依赖项

<dependency>
    <groupId>javaee</groupId>
    <artifactId>javaee-api</artifactId>
    <version>5</version>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.1.0</version>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>javax.servlet.jsp</groupId>
    <artifactId>javax.servlet.jsp-api</artifactId>
    <version>2.3.1</version>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>javax.validation</groupId>
    <artifactId>validation-api</artifactId>
    <version>1.0.0.GA</version>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>org.jboss.ejb3</groupId>
    <artifactId>jboss-ejb3-ext-api</artifactId>
    <version>1.0.0</version>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <version>4.3.7.Final</version>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>4.3.7.Final</version>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-commons-annotations</artifactId>
    <version>3.3.0.ga</version>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-annotations</artifactId>
    <version>3.5.6-Final</version>
    <scope>provided</scope>
</dependency>            
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>3.1.0.GA</version>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>org.hsqldb</groupId>
    <artifactId>hsqldb</artifactId>
    <version>2.3.2</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.11</version>
    <scope>test</scope>

插件:

<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
    </plugin>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.3.2</version>
        <configuration>
            <source>1.7</source>
            <target>1.7</target>
        </configuration>
    </plugin>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <configuration>
            <includes>
                <include>**/MyTestSuite.class</include>
            </includes>
        </configuration>
    </plugin>
</plugins>

尝试了很多选项,删除了测试范围等提供的所有选项,但仍然会出现同样的错误。

2 个答案:

答案 0 :(得分:-1)

问题可能会消失:

  • 重新排序/更新某些依赖项
  • 创建新依赖项:
<dependency>
    <groupId>org.apache.geronimo.specs</groupId>
    <artifactId>geronimo-ejb_3.0_spec</artifactId>
    <version>1.0.1</version>
    <scope>test</scope>
</dependency>

答案 1 :(得分:-2)

尝试将surefire plugin configuration <plugin>用于pom.xml。你可以在下面找到插件:

<plugin>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.12</version>
    <configuration>
        <classpathDependencyExcludes>
            <!-- exclude code absent api -->
            <classpathDependencyExclude>javax.faces:javax.faces-api</classpathDependencyExclude>
        </classpathDependencyExcludes>
    </configuration>
</plugin>