无法实例化 javax.servlet.ServletException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2979968/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can't instantiate javax.servlet.ServletException
提问by Denis
I am trying to create instance of class javax.servlet.ServletExceptionwith following code
我正在尝试使用以下代码创建类javax.servlet.ServletException 的实例
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:
Maven 帮助我解决依赖关系:
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>6.0</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
What am I doing wrong?
我究竟做错了什么?
采纳答案by Pascal Thivent
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):
正如@user353852 所提到的,您当前的依赖项仅包含 Java EE 6 API,不包含任何方法主体。所以你不能针对它运行代码。要在容器外运行代码,您需要获得“具体”依赖项(来自 GlassFish 存储库):
<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
).
请注意,不应使用compile
范围声明此类依赖项,您不想捆绑它(它应该provided
或可能是test
,但不是compile
或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.
我想知道 javaee 实现的提供者重要吗?通常我使用 Apache 服务器,因此拥有与服务器上相同的 javaee 实现会很棒。
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 .
理论上,没有。但在实践中,我建议使用来自您将要使用的服务器(或来自 Java EE 参考实现)的实现 JAR。由于您使用的是 Java EE 6,这实际上意味着在这两种情况下都是 GlassFish v3 中的 JARS。
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".
第二个问题更为重要。javax.servlet 只是 javaee-api 实现的一部分,我在哪里可以找到其他部分。现在我需要“javax/验证/验证”。
For the Bean Validation API, you'll need the following (Hibernate Validator being the RI):
对于 Bean Validation API,您需要以下内容(Hibernate Validator 作为 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?
我如何确定哪个工件实现了 javaee 的每个方面。也许某处有某种“地图”?
Nothing official but this nice answerfrom BalusC will help.
没有任何官方消息,但BalusC 的这个不错的答案会有所帮助。
回答by krock
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 realjar file, i.e. the one out of your servlet container.
看看这篇文章。基本上,这些 Maven 库是存根,仅适用于编译。这些并不意味着在运行时被引用。在运行时(即使是单元测试),您将需要引用真正的jar 文件,即 servlet 容器中的那个。