Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
435 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...