Can't instantiate javax.servlet.ServletException

Denis picture Denis · Jun 5, 2010 · Viewed 21.7k times · Source

I am trying to create instance of class javax.servlet.ServletException with following code

public class MyTroubleViewer {
 public static void main(String[] args) {
  javax.servlet.ServletException servletException = new javax.servlet.ServletException("Hello");
  System.out.println(servletException.getMessage());
 }
}

But I get exception on creating:

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

Maven helps me with dependecies:

<dependency>
 <groupId>javax</groupId>
 <artifactId>javaee-api</artifactId>
 <version>6.0</version>
 <type>jar</type>
 <scope>compile</scope>
</dependency>

What am I doing wrong?

Answer

Pascal Thivent picture Pascal Thivent · Jun 5, 2010

As mentioned by @user353852, your current dependency contains only the Java EE 6 APIs and does not contain any method bodies. So you can't run code against it. To run your code outside a container, you need to get a "concrete" dependency (from GlassFish repository):

<repositories>
  <repository>
    <id>glassfish-repository</id>
    <url>http://download.java.net/maven/glassfish</url>
  </repository>
  ...
</repositories>

<dependencies>
  <dependency>
    <groupId>org.glassfish</groupId>
    <artifactId>javax.servlet</artifactId>
    <version>3.0</version>
    <scope>test</scope>
  </dependency>
  ...
</dependencies>

Note that such dependencies shouldn't be declared with a compile scope, you don't want to bundle it (it should be provided or maybe test, but not compile or runtime).


I wonder does the provider of the javaee implementation important? Generally I use Apache servers, so it will be great to have the same javaee implementation as it is on the server.

In theory, no. But in practice, I would recommend to use the implementation JARs from the server you are going to use (or from the Java EE Reference Implementation). Since you are using Java EE 6, this actually means JARS from GlassFish v3 in both cases .

The second question is much more vital. javax.servlet is only one part of javaee-api implementation, where can I find the others. Now I need "javax/validation/Validation".

For the Bean Validation API, you'll need the following (Hibernate Validator being the RI):

<repositories>
  <!-- For Hibernate Validator -->
  <repository>
    <id>jboss</id>
    <name>JBoss repository</name>
    <url>http://repository.jboss.org/maven2</url>
  </repository>
  ...
</repositories>

<dependencies>
  <!-- Bean Validation API and RI -->
  <dependency>
    <groupId>javax.validation</groupId>
    <artifactId>validation-api</artifactId>
    <version>1.0.0.GA</version>
    <scope>provided</scope>
  </dependency>
  <dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>4.0.2.GA</version>
    <scope>runtime</scope>
  </dependency>
  ...
</dependencies>

How can I determine which artifact implements each aspect of javaee. Maybe there is some kind of a "map" somewhere?

Nothing official but this nice answer from BalusC will help.