[英]Embedded Jetty Absent Code attribute in method that is not native or abstract in class file javax/servlet/ServletException
[英]Absent Code attribute in method that is not native or abstract in class file javax/servlet/ServletException
我打算在我的應用程序中使用Java servlet。 我在我的項目的POM.xml文件中包含以下內容以加載Java servlet 3.0實現jar。
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.servlet</artifactId>
<version>3.2-b05</version>
</dependency>
該項目編譯得很好。 但是,當我運行它時,我收到以下錯誤:
java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/servlet/ServletException
我在這里搜索它並找到了一些很好的答案 。
我從他們中發現,當我們包含僅包含由servlet API定義的接口而不是實際實現的JAR時,會發生此錯誤。 所以,我檢查了我正在使用的glassfish jar只是接口或它也包含實現。 我發現它是一個實現而不僅僅是接口。
所以現在,我想知道為什么我在運行時收到此錯誤。 任何人?
更新:
剛才,我發現這是我身邊一個明顯的錯誤(我正在將jar添加到一個項目中,而正在運行一個完全不同的項目!)。 對不起,我很抱歉。 添加glassfish servlet實現可以解決問題。
謝謝,Sandeep
我在過去2個小時左右的時間里一直在與一個與用於surefire插件的javaee-api和javaee-web-api依賴項相關的問題進行斗爭。 正如JBoss論壇上的人們剛才發布的那樣,看來整個JEE6庫被划分(根據Sun / Oracle的決定)分為API(僅限接口/存根)JAR和提供者。
這與此有何關系? 如果您遇到問題,比如FacesContext類 ,則會出現如下錯誤:
java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/faces/context/FacesContext
如果你看一下依賴樹,你會在編譯類路徑中找到一個默認的API JAR,它也會影響運行時的問題:
javax.faces:javax.faces-api:jar:2.1:provided
為surefire插件配置添加顯式排除將在測試時強制使用提供程序JAR依賴項:
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12</version>
<configuration>
<classpathDependencyExcludes>
<!-- exclude code absent api -->
<classpathDependencyExclude>javax.faces:javax.faces-api</classpathDependencyExclude>
</classpathDependencyExcludes>
</configuration>
</plugin>
希望有所幫助,它確實對我有用。
我交易到glassfish-embedded-all並解決了這個問題。
<dependency>
<groupId>org.glassfish.main.extras</groupId>
<artifactId>glassfish-embedded-all</artifactId>
<version>3.1.2.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.main.extras</groupId>
<artifactId>glassfish-embedded-all</artifactId>
<version>3.1.2.2</version>
<scope>provided</scope>
它對我有用。 謝謝。 但是pom.xml中的順序對我來說也很重要
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.main.extras</groupId>
<artifactId>glassfish-embedded-all</artifactId>
<version>3.1.2.2</version>
<scope>test</scope>
</dependency>
以上順序不起作用
<dependency>
<groupId>org.glassfish.main.extras</groupId>
<artifactId>glassfish-embedded-all</artifactId>
<version>3.1.2.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
以上訂單工程
當我運行我的測試(JUnit + Mockito)時,我特別使用了Jersey。 對我有用的是將下面的代碼添加到我的pom.xml文件中。
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-test-framework</artifactId>
<version>1.1.5.1</version>
<scope>test</scope>
</dependency>
注意:我正在使用Jersey 1.17
我最近遇到了同樣的錯誤,感謝這個問題和上面的答案 - 特別是leandro.freitos - 我能夠使用它來解決它
<dependency>
<groupId>org.glassfish.main.extras</groupId>
<artifactId>glassfish-embedded-all</artifactId>
<version>3.1.2.2</version>
<scope>provided</scope>
</dependency>
原來我的javax.servlet也有
我有一個類似於josdem的情況(同樣的錯誤,同時運行JUnit與Mockito),但沒有澤西島。 所以這是一個獨立的澤西島解決方案,對我有用:
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-servlet_3.0_spec</artifactId>
<version>1.0</version>
<scope>test</scope>
</dependency>
同樣的問題在這里 雖然我發現這是我聲明依賴關系的順序。 需要在提供的javaee-web-api依賴項之前聲明glassfish嵌入式依賴項。
<dependency>
<groupId>org.glassfish.extras</groupId>
<artifactId>glassfish-embedded-all</artifactId>
<version>3.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
當測試中的javaee-web-api之后放置glassfish嵌入時,我不確定為什么類路徑會變得混亂。 我想JVM嘗試先解決首先提供的javax類,然后在測試期間放棄。 我認為宣布測試范圍優先,但在我的情況下似乎並非如此。 希望這有助於某人。
用netbeans 7.2.1編譯同樣的問題。 但是輸出指定我自己的一個java源文件具有“缺少代碼屬性.......等”
同時,我可以使用JDeveloper編譯和運行相同的項目。 經過一些“清理”並重新啟動后,netbeans仍然引發了同樣的問題。
我最后通過在報告為具有“缺少代碼屬性”並使用與調試目標相同的java中添加main方法來修復它。 全部恢復正常。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.