ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/mail/MessagingException

跟風遠走 提交于 2019-11-27 04:31:18
Bill Shannon

The javax:javaee-api is intended for compiling, not running, including unit tests. If you need classes suitable for running against, run against a Java EE application server.

Also, the correct name is Java EE, not JEE.

I was having a similar issue,... so, when I was running JUnit an error occured:

Absent Code attribute in method that is not native or abstract in class file javax/servlet/http/HttpServlet

I solve it adding the following dependency (first one):

<dependency>
    <groupId>org.glassfish.main.extras</groupId>
    <artifactId>glassfish-embedded-all</artifactId>
    <version>3.1.2.2</version>
    <scope>test</scope>
</dependency>
Perception

You normally get this error when you have multiple conflicting JAR's in your class path. One of the more common reasons is if you include an artifact in your classpath that is supposed to be provided by the application container. Looking at your POM, you have several redundant and overlapping dependencies. Remove these:

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>servlet-api</artifactId>
    <version>2.5</version>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>javax.validation</groupId>
    <artifactId>validation-api</artifactId>
    <version>1.0.0.GA</version>
</dependency>
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
</dependency>
<dependency>
    <groupId>javax.mail</groupId>
    <artifactId>mail</artifactId>
    <version>1.4.3</version>
</dependency>
<dependency>
    <groupId>javaee</groupId>
    <artifactId>javaee-api</artifactId>
    <version>5</version>
</dependency>

And change the scope of your Java EE dependency to 'provided':

<dependency>
    <groupId>javax</groupId>
    <artifactId>javaee-api</artifactId>
    <version>6.0</version>
    <scope>provided</scope>
</dependency>

Finally, make sure you are deploying to a Java EE 6 capable web server, and the error should go away.

--- Edit ---

Just noticed that you are compiling to a Java 5 compliance level. Change this:

<configuration>
    <source>1.5</source>
    <target>1.5</target>
</configuration>

And make the source and target values 1.6.

Sakshi Goel

I too faced the same kind of exception when running junits with jmock and maven:

Exception in constructor: testDoExecute (java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/servlet/http/Cookie at java.lang.ClassLoader.defineClass1(Native Method)

It is due to classes your code try to access which are not available in the javaee api jar used in the project because your code compiles against incomplete classes, like the JavaEE6 Api and your Unit tests try to access code thats not there. JUnit will simply fail and mark the test as error, printing something like the above exception.

It could be solved either by adding javax.servlet jar/dependency or changing javaee jar version shown below:

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

**OR** 

<dependency>
    <groupId>javax.j2ee</groupId>
    <artifactId>j2ee</artifactId>
    <version>1.4</version>
    <scope>provided</scope>
</dependency>

PS: Please add the jar version compatible with your other existing jars in the project.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!