24

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

Community
  • 1
  • 1
schauhan
  • 531
  • 1
  • 5
  • 17
  • 2
    You shouldn't include the servlet jar file in your deployed dependencies, since GlassFish already has it in its classpath. This dependency should have the "provided" scope. – JB Nizet May 22 '12 at 21:07
  • @JBNizet: I am not using Glassfish as servlet container. I am using Jetty for that. – schauhan May 23 '12 at 04:03
  • Jetty jars (for server, utils, servlet etc) do not seem to have any implementation of Servlet API. So if I do not explicitly include the implementation jar, how will it work? – schauhan May 23 '12 at 07:08
  • 2
    How could Jetty be a servlet container by not instantiating the servlet requests and responses it passes to your servlet? – JB Nizet May 23 '12 at 07:10

8 Answers8

17

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.

RickB
  • 655
  • 1
  • 7
  • 11
  • 2
    Thank you! Had a similar problem with javax from the javaee-web-api. Excluding it like you described (but then javax:javaee-web-api) solved it. – GreatBittern May 20 '13 at 09:04
8

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>
frekele
  • 644
  • 6
  • 13
4
<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

Dhaval Patel
  • 141
  • 1
  • 9
3

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

josdem
  • 196
  • 2
  • 8
3

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

2

I had a similar case to the one josdem had (same error, also while running JUnit with Mockito), but without Jersey. So here's a Jersey-independent solution that worked for me:

    <dependency> 
        <groupId>org.apache.geronimo.specs</groupId>
        <artifactId>geronimo-servlet_3.0_spec</artifactId>
        <version>1.0</version>
        <scope>test</scope>
    </dependency>
machinery
  • 3,793
  • 4
  • 41
  • 52
1

Same problem here. Though it turned out for me to be the order in which I was declaring dependencies. The glassfish embedded dependency needs to be declared ahead of the provided javaee-web-api dependency.

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

I'm not sure why the class path gets muddled when the glassfish embedded is placed after the javaee-web-api in tests. I suppose the JVM tries to resolve the javax classes in the provided first and then gives up during testing. I would figure that declaring scope for test would take precedence but that appears to not be the case in my situation. Hope this helps someone.

Hodglem
  • 614
  • 9
  • 17
-2

got same problem compiling with netbeans 7.2.1. However the output specified one of my own java source files as having "absent code attribute.......etc"

At same time, I could compile and run same project using JDeveloper. After a few "cleans" and restarts the netbeans still threw up the same problem.

I finally fixed it by adding a main method into the java being reported as having "absent code attribute" and using same as the debug target. All returned to normal.

  • however it continued to pop up again and finally my massaging techniques failed. I then downloaded glassfish and added javaee.jar to my netbeans project library with immediate success. – Vince Stewart Jun 18 '13 at 23:45