2011-08-16 117 views
51

我收到一個異常,我無法找到它的原因。java.lang.IllegalAccessError:試圖訪問方法

我得到的例外是:

java.lang.IllegalAccessError: tried to access method Connected.getData(Ljava/lang/String;)Ljava/sql/ResultSet; from class B

的方法是公開的。

public class B 
{ 
    public void myMethod() 
    { 
    Connected conn = new Connected(); // create a connected class in order to connect to The DB 
    ResultSet rs = null; // create a result set to get the query result 
    rs = conn.getData(sql); // do sql query 
    } 
} 

public class Connected 
{ 
public ResultSet getData(String sql) 
{ 
    ResultSet rs = null; 
    try 
    { 
    prepareConnection(); 
    stmt = conn.createStatement(); 
    stmt.execute(sql); 
    rs = stmt.getResultSet(); 
    } 
    catch (SQLException E) 
     { 
    System.out.println("Content.getData Error"); 
    E.printStackTrace(); 
     } 
return rs; 
} 

我使用的Apache Tomcat 5.5.12 和JAVA 1.6

回答

56

你幾乎肯定使用不同版本的類在運行時你期望的人。特別是,運行時類將與您編譯的類不同(否則這會導致編譯時錯誤) - 有沒有這種方法已被private?你的系統上的任何地方都有老版本的類/罐子嗎?

由於javadoc,瞭解IllegalAccessError狀態,

Normally, this error is caught by the compiler; this error can only occur at run time if the definition of a class has incompatibly changed.

,我肯定會看你的類路徑,並檢查其是否持有任何驚喜。

+3

@yossi:那麼你是如何解決它的?重新編譯soruce並將其放入你的類路徑中?只是好奇你是否反編譯了原始課程,並將其中的內容翻過來。 – Victor

+0

當我有這個,我已經保存了一個私有函數公開的文件,所以編輯器在.jsp文件中看到一切都沒問題,但我沒有重新啓動主程序,所以它沒有實際編譯新的版。 –

+0

我收到剛剛安裝的maven軟件包中的這個錯誤。我已經更新了項目並清理了它,但似乎任何東西都發生了變化.. – softwareplay

5

如果getData受保護,請嘗試將其公開。這個問題可能存在於JAVA 1.6中,並且在1.5x中不存在問題

我爲你的問題得到了這個。 Illegal access error

75

訪問位於同一個包但位於不同jar和classloader中的類的包範圍方法時會發生這種情況。

This是我的來源,但現在鏈接已斷開。以下是來自谷歌的緩存全文:

Packages (as in package access) are scoped per ClassLoader.

You state that the parent ClassLoader loads the interface and the child ClassLoader loads the implementation. This won't work because of the ClassLoader-specific nature of package scoping. The interface isn't visible to the implementation class because, even though it's the same package name, they're in different ClassLoaders.

I only skimmed the posts in this thread, but I think you've already discovered that this will work if you declare the interface to be public. It would also work to have both interface and implementation loaded by the same ClassLoader.

Really, if you expect arbitrary folks to implement the interface (which you apparently do if the implementation is being loaded by a different ClassLoader), then you should make the interface public.

The ClassLoader-scoping of package scope (which applies to accessing package methods, variables, etc.) is similar to the general ClassLoader-scoping of class names. For example, I can define two classes, both named com.foo.Bar, with entirely different implementation code if I define them in separate ClassLoaders.

Joel

+1

我在JBoss EAP 5.3下的commons-collections-3.2.1.jar下有這個問題,因爲這個JAR已經在JBoss中庫,但也包裝在我的應用程序中。我從解決問題的應用程序中刪除了JAR。 –

+2

你剛剛救了我很多時間和神經。非常好! – msteiger

+0

即時獲取此錯誤的公共靜態方法,現在需要第二個參數。舊的jar文件不再存在,因爲我將其替換。有任何想法嗎? – Markus

1

這發生在我身上時,我在一個罐子試圖從另一個罐子訪問私有方法的類有一個類。我只是將私有方法更改爲公共,重新編譯和部署,然後它就可以正常工作。

0

從Android的角度來看: 方法在API版本不提供

我主要是讓這個問題,因爲我使用了一些東西,不可用/在Android版本

錯誤棄用方法:

Notification.Builder nBuilder = new Notification.Builder(mContext); 
nBuilder.addAction(new Notification.Action(android.R.drawable.ic_menu_view,"PAUSE",pendingIntent)); 

正確的做法:

Notification.Builder nBuilder = new Notification.Builder(mContext); 
nBuilder.addAction(android.R.drawable.ic_media_pause,"PAUSE",pendingIntent); 

此處通知。操作不可用之前API 20和我分的版本是API 16

0

只是一個除了解決答案:

這可能與Android Studio的即時運行特徵的問題,例如,如果您意識到忘記在打開另一個代碼後忘記添加代碼行:finish()並且您已經重新打開了您不應該重新打開的活動(其中finish()已解決),那麼您將添加finish()和Instant運行發生,然後應用程序將崩潰,因爲邏輯已被破壞。


TL:DR;

這不一定是一個代碼問題,只是一個即時運行的問題

2

我在春天啓動應用程序,其中一個@RestController ApplicationInfoResource有一個嵌套類ApplicationInfo收到此錯誤。

看來Spring Boot Dev Tools正在使用不同的類加載器。

的例外,我是越來越

2017-05-01 17:47:39.588 WARN 1516 --- [nio-8080-exec-9] .m.m.a.ExceptionHandlerExceptionResolver : Resolved exception caused by Handler execution: org.springframework.web.util.NestedServletException: Handler dispatch failed; nested exception is java.lang.IllegalAccessError: tried to access class com.gt.web.rest.ApplicationInfo from class com.gt.web.rest.ApplicationInfoResource$$EnhancerBySpringCGLIB$$59ce500c

解決方案

我感動的嵌套類ApplicationInfo到一個單獨的java文件,並擺脫了問題。

相關問題