This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Java in General and the fly likes IllegalAccessError and Classloaders. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "IllegalAccessError and Classloaders." Watch "IllegalAccessError and Classloaders." New topic
Author

IllegalAccessError and Classloaders.

Jason Pepper
Greenhorn

Joined: Oct 15, 2005
Posts: 9
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

Joined: Jan 30, 2000
Posts: 18671
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.


"I'm not back." - Bill Harding, Twister
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: IllegalAccessError and Classloaders.
 
Similar Threads
doubt in access modifiers...
static inner class question.
static block and empty block inside any class.
Wetware failure and Singletons
VerifyError: Bad access to protected data