Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.IllegalAccessError: tried to access method

Tags:

java

exception

I am getting an exception and I can't find the reason of it.

The exception I get is :

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

The method is public.

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; } 

i am using apache tomcat 5.5.12 and JAVA 1.6

like image 903
yossi Avatar asked Aug 16 '11 09:08

yossi


People also ask

What is Java Lang IllegalAccessError?

Class IllegalAccessErrorThrown if an application attempts to access or modify a field, or to call a method that it does not have access to. 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.

How to solve Illegal access exception in java?

IllegalAccessException. If you run the above program, you will get the following exception trace as the output. The solution for this problem is to check the method or constructor whether it has the proper access modifier to access in your application.


2 Answers

This happens when accessing a package scoped method of a class that is in the same package but is in a different jar and classloader.

This was my source, but the link is now broken. Following is full text from google cache:

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

like image 167
s_t_e_v_e Avatar answered Oct 07 '22 00:10

s_t_e_v_e


You are almost certainly using a different version of the class at runtime to the one you expect. In particular, the runtime class would be different to the one you've compiled against (else this would have caused a compile-time error) - has that method ever been private? Do you have old versions of the classes/jars on your system anywhere?

As the javadocs for IllegalAccessError state,

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.

I'd definitely look at your classpath and check whether it holds any surprises.

like image 33
Andrzej Doyle Avatar answered Oct 06 '22 23:10

Andrzej Doyle