java.lang.IllegalAccessError : tried to access method

标签 java exception

我遇到异常,但找不到原因。

我得到的异常(exception)是:

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

最佳答案

当访问在同一个包中但在不同 jar 和类加载器中的类的包作用域方法时会发生这种情况。

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

关于java.lang.IllegalAccessError : tried to access method,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7076414/

相关文章:

c# - WebSockets-尖锐异常: WebSocketException

java - 设置外观时序列化 AbstractTableModel 失败

java - 如果 @SuppressWarnings 抑制的 Checkstyle 规则违规未抑制任何内容,则发出警告

java - 责任链 [GoF] 缺点

multithreading - 处理 TThread.Execute 中的异常以使其不可中断

android - 没有此功能时获取下一个警报信息

c++ - 为什么抛出局部变量调用 moves 构造函数?

Java 扫描器资源泄漏,.close() 不会因多次分配而关闭

java - 将 2D CharArray 转换为 String 并使用 .charAt() 比较字符是否正确?

java - 如何使用 Java 在 selenium webdriver 中测试或模拟将文件从桌面拖动到浏览器