java.lang.IllegalAccessError: tried to access method

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

Leave a Comment