简体   繁体   中英

Why do I get an IllegalAccessError when loading a class remotely?

Note: I am not very familiar with programming, I hope you don't mind my incorrect use of terms. :-)

I am trying to load a class remotely. It seems that the class gets downloaded and initiated correctly, but I get an IllegalAccessError when calling one of its methods. Here's what I do locally, this works 100 % without an error.

(Parent) instance = (Parent)Class.forName("somepackage.ChildClass").newInstance();
instance.callSomeMethod();

Here's what I do to load it remotely. This gives an IllegalAccessError, more specifically "tried to access field Parent.field from class ChildClass"

URLClassLoader classLoader = new URLClassLoader(new URL[] { new URL("http://mysite.com/classes/") });
(Parent) instance = (Parent)classLoader.loadClass("somepackage.ChildClass").newInstance();
instance.callSomeMethod();

Also please note that all local classes are identical to the remote/internet ones and that I do not want to download and save the file to my classpath.

I think that if you use different class loaders, then child class may not actually be an instance of parent. So if you have loaded Parent in one class loader and Child in the other, it may be causing your problems if the child class is work with/on parent objects from the other class loader.

Please consider this answer to be somewhat suspect since I am not exactly sure if it applies to your situation (it would be helpful to see the details of the method that is failing and the declaration of the field that cannot be accessed).

An IllegalAccessError happens when your code (some how) attempts to access a field or call a method that the access modifiers say you can't do.

Normally, you get a Java compilation error when you try to do this. But it appears that your "remote loading" is doing an end-run around the static checks ...

I can think of two possible explanations, but it is difficult to distinguish them without more information; eg actual code and actual stacktraces.

  • You could be remote loading a version of the class that is different to the local one, and the method / field you are trying to use has different access. (You can't subvert the compiler's access checks this way ... it that's what you are really trying to do.)

  • You may have both the local and remote copies of the class in your JVM. The problem here is that the two versions will be different classes from the perspective of the type system. (Yes, two different classes with the same FQN, and maybe even identical code.) This might result in access problems.

I'm more inclined to think it is the first problem, because I think the second one would manifest as a IllegalAccessException rather than an IllegalAccessError .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM