Can't instantiate javax.servlet.ServletException
Asked Answered
S

3

14

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?

Systematism answered 5/6, 2010 at 10:27 Comment(0)
E
22

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.

Echoism answered 5/6, 2010 at 13:58 Comment(4)
Pascal, can you help me a bit more? 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. But it is not so important. 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". How can I determine which artifact implements each aspect of javaee. Maybe there is some kind of a "map" somewhere?Systematism
I found "maven.apache.org/guides/mini/guide-coping-with-sun-jars.html" but it seems that they have changed content of the repository, cos I failed to find "javax.j2ee:j2ee" artifact that was metioned on this page.Systematism
@Systematism The page mentioned in your last comment is totally out of date. Ignore it.Echoism
Old post, I know, but I just had the same problem with the javax validation libraries. Eclipse WTP setup up my project with the Glassfish libraries separate from the Maven Dependencies libraries, so I could build in Eclipes, but when I tried mvn clean install from the command line, I got the ClassFormatError. More than a year later and your post is still helping, Pascal. Thanks!Kataway
T
5

Check out this post. Basically these maven libraries are stubs and are good for compiling against only. These are not meant to be referenced at runtime. At runtime (even for unit tests) you will need to reference the real jar file, i.e. the one out of your servlet container.

Thankyou answered 5/6, 2010 at 10:38 Comment(1)
I get it. Thanks. But I still have one problem. Javaee is required during testing stage before deploying to web server. Does it mean that I should have JavaEE implementation in my system's classpath, or there is a comfortable way of doing it with maven?Systematism
L
0

ensure 1. servlet class is declared as public. 2. the path is correctly specified in web.xml or otherwise use annotations.

Laaland answered 31/5, 2020 at 7:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.