Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

servlets

I am planning to use Java servlets in my application. I included the following in my project's POM.xml file to load Java servlet 3.0 implementation jar.

<dependency>
    <groupId>org.glassfish</groupId>
    <artifactId>javax.servlet</artifactId>
    <version>3.2-b05</version>
</dependency> 

The project compiles fine. However, when I run it, I get the following error:

java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/servlet/ServletException

I searched for it here and found some good answers.

I figured out from them that this error happens when we include the JAR which contains only interfaces defined by servlet API and not the actual implementation. So, I checked that the glassfish jar I am using is just interfaces or it contains implementation too. I found that it is an implementation and not just interfaces.

So now, I am wondering why I am getting this error at runtime. Anyone?

UPDATE:

Just now, I found out that it was a blatant error from my side (I was adding the jar to one project, while, was running an altogether different project!). I am sorry for this. Adding the glassfish servlet implementation DOES solve the issue.

Thanks, Sandeep

like image 351
schauhan Avatar asked May 22 '12 20:05

schauhan


5 Answers

I have been fighting the last 2 hours or so with an issue related to the javaee-api and javaee-web-api dependencies used for surefire plugin. As the guys at JBoss forum kindly posted a while ago, it appears as the whole JEE6 library was divided (as per Sun/Oracle decision) into an API (interfaces/stubs only) JAR and the providers.

How does that relate to this? If you have a problem with, say FacesContext class, you get an error like this:

java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/faces/context/FacesContext

If you take a look at the dependency tree you'll find a default API JAR in the compile classpath that is also getting in the way for runtime matters:

javax.faces:javax.faces-api:jar:2.1:provided

Adding an explicit exclusion for surefire plugin configuration will enforce the provider JAR dependencies usage at test time:

<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>

Hope that helps, it did work for me.

like image 188
RickB Avatar answered Oct 31 '22 19:10

RickB


I traded to glassfish-embedded-all and resolved this.

    <dependency>
        <groupId>org.glassfish.main.extras</groupId>
        <artifactId>glassfish-embedded-all</artifactId>
        <version>3.1.2.2</version>
        <scope>provided</scope>
    </dependency>
like image 26
frekele Avatar answered Oct 31 '22 18:10

frekele


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

It worked for me. Thanks. But order in pom.xml also matters as for me

 <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-api</artifactId>
        <version>6.0</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.glassfish.main.extras</groupId>
        <artifactId>glassfish-embedded-all</artifactId>
        <version>3.1.2.2</version>
        <scope>test</scope>
    </dependency>

Above order Does not Work

<dependency>
        <groupId>org.glassfish.main.extras</groupId>
        <artifactId>glassfish-embedded-all</artifactId>
        <version>3.1.2.2</version>
        <scope>test</scope>
    </dependency>
 <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-api</artifactId>
        <version>6.0</version>
        <scope>provided</scope>
    </dependency>

Above Order Works

like image 35
Dhaval Patel Avatar answered Oct 31 '22 17:10

Dhaval Patel


I faced same error using Jersey specifically when I run my tests (JUnit + Mockito). What works for me was adding the code below to my pom.xml file.

<dependency>
   <groupId>com.sun.jersey</groupId>
   <artifactId>jersey-test-framework</artifactId>
   <version>1.1.5.1</version>
   <scope>test</scope>
</dependency>

Note: I'm using Jersey 1.17

like image 3
josdem Avatar answered Oct 31 '22 18:10

josdem


I recently encountered this same error and, thanks to this question and the above answers - especially leandro.freitos - I was able to resolve it using

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

Turns out mine had to do with javax.servlet

like image 3
Chuck Ludwigsen Avatar answered Oct 31 '22 19:10

Chuck Ludwigsen