java.lang.IllegalAccessError: tried to access method

JavaException

Java Problem Overview


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

Java Solutions


Solution 1 - Java

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

Solution 2 - Java

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.

Solution 3 - Java

If getData is protected then try making it public. The problem could exist in JAVA 1.6 and be absent in 1.5x

I got this for your problem. http://jira.codehaus.org/browse/GROOVY-2493">Illegal access error

Solution 4 - Java

This happened to me when I had a class in one jar trying to access a private method in a class from another jar. I simply changed the private method to public, recompiled and deployed, and it worked ok afterwards.

Solution 5 - Java

I was getting this error on a Spring Boot application where a @RestController ApplicationInfoResource had a nested class ApplicationInfo.

It seems the Spring Boot Dev Tools was using a different class loader.

The exception I was getting > 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

Solution

I moved the nested class ApplicationInfo to a separate .java file and got rid of the problem.

Solution 6 - Java

I was getting same error because of configuration issue in intellij. As shown in screenshot. Main and test module was pointing to two different JDK. (Press F12 on the intellij project to open module settings)

Also all my dto's were using @lombok.Builder which I changed it to @Data.

enter image description here

enter image description here

Solution 7 - Java

From Android perspective: Method not available in api version

I was getting this Issue primarily because i was using some thing that is not available/deprecated in that Android version

Wrong way:

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

Right way:

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

here Notification.Action is not available prior to API 20 and my min version was API 16

Solution 8 - Java

Just an addition to the solved answer:

This COULD be a problem with Android Studio's Instant Run feature, for example, if you realized you forgot to add the line of code: finish() to your activity after opening another one, and you already re-opened the activity you shouldn't have reopened (which the finish() solved), then you add finish() and Instant Run occurs, then the app will crash since the logic has been broken.


TL:DR;

This is not necessarily a code problem, just an Instant Run problem

Solution 9 - Java

In my case the problem was that a method was defined in some Interface A as default, while its sub-class overrode it as private. Then when the method was called, the java Runtime realized it was calling a private method.

I am still puzzled as to why the compiler didn't complain about the private override..

public interface A {
     default void doStuff() {
         // doing stuff
     }
}

public class B {
     private void doStuff() {
         // do other stuff instead
     }
}

public static final main(String... args) {
    A someB = new B();
    someB.doStuff();
}

Solution 10 - Java

In my case I was getting this error running my app in wildfly with the .ear deployed from eclipse. Because it was deployed from eclipse, the deployment folder did not contain an .ear file, but a folder representing it, and inside of it all the jars that would have been contained in the .ear file; like if the ear was unzipped.

So I had in on jar:

class MySuperClass {
  protected void mySuperMethod {}
}

And in another jar:

class MyExtendingClass extends MySuperClass {

  class MyChildrenClass {
    public void doSomething{
      mySuperMethod();
    }
  }
}

The solution for this was adding a new method to MyExtendingClass:

class MyExtendingClass extends MySuperClass {

  class MyChildrenClass {
    public void doSomething{
      mySuperMethod();
    }
  }
  
  @Override
  protected void mySuperMethod() {
    super.mySuperMethod();
  }
}

Solution 11 - Java

I was getting similar exception but at class level

e.g. Caused by: java.lang.IllegalAccessError: tried to access class ....

I fixed this by making my class public.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionyossiView Question on Stackoverflow
Solution 1 - Javas_t_e_v_eView Answer on Stackoverflow
Solution 2 - JavaAndrzej DoyleView Answer on Stackoverflow
Solution 3 - JavaShadowView Answer on Stackoverflow
Solution 4 - JavaAlan SmithView Answer on Stackoverflow
Solution 5 - Javagtiwari333View Answer on Stackoverflow
Solution 6 - JavaVinayak DornalaView Answer on Stackoverflow
Solution 7 - JavaMauriceView Answer on Stackoverflow
Solution 8 - JavaAli BdeirView Answer on Stackoverflow
Solution 9 - JavaNeuron - Freedom for UkraineView Answer on Stackoverflow
Solution 10 - Javadaniel spView Answer on Stackoverflow
Solution 11 - JavariteshkView Answer on Stackoverflow