views:

344

answers:

2

Hello, experts!

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?

+2  A: 

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.

krock
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?
Denis
+3  A: 

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&lt;/url&gt;
  </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&lt;/url&gt;
  </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.

Pascal Thivent
Thanks, it works!
Denis
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?
Denis
I found "http://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.
Denis
@Denis The page mentioned in your last comment is totally out of date. Ignore it.
Pascal Thivent
Great thanks!!!
Denis