Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

IllegalAccessError and Classloaders.

 
Jason Pepper
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
package bar;
public class Foo {
protected void doit() {
StaticClass.doit1();
}
}

package bar;
final class StaticClass {
static void doit1() {

}
}

StaticClass is in a higher classloader than class Foo. When I call doit() on a foo object I get an IIlegalAccessError.. Whats going on because my IDE
did not complain about this and usually something like this should be caught
at compile time which makes me suspect that its a classloader issue.

Can Foo access a package leve method of package level class defined in a higher class loader?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you say StaticClass is in a higher classloader, you mean it's defined in the parent classloader (or parent of parent, etc) of Foo's classloader? Then that should not be a problem. I suspect your problem is that the method doit() (not doit1()) is protected, and you're now accessing it from outside the package. Try calling setAccessible(true) on the Method object, before calling invoke().

If that doesn't work, examine the stack trace carefully. Is it complaining about being unable to access doit(), or doit1()? If you're not sure, post the stack trace here.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic