简体   繁体   English

为什么在远程加载类时出现IllegalAccessError?

[英]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. 似乎该类已正确下载并启动,但是在调用其方法之一时出现IllegalAccessError。 Here's what I do locally, this works 100 % without an error. 这是我在本地执行的操作,它可以100%无错误地工作。

(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" 这给出了一个IllegalAccessError,更具体地说是“试图从类ChildClass访问字段Parent.field”

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. 因此,如果您在一个类加载器中加载了Parent,而在另一个类加载器中加载了Child,那么如果子类与其他类加载器中的父对象一起工作,则可能会导致问题。

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. 当您的代码(以某种方式)尝试访问字段或调用访问修饰符认为您无法执行的方法时,会发生IllegalAccessError。

Normally, you get a Java compilation error when you try to do this. 通常,尝试执行此操作时会遇到Java编译错误。 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. 您可能在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. (是的,两个不同的类具有相同的FQN,甚至可能是相同的代码。)这可能会导致访问问题。

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 . 我更倾向于认为这是第一个问题,因为我认为第二个问题将表现为IllegalAccessException而不是IllegalAccessError

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么在TomEE上远程调用EJB时​​会出现AuthenticationException? - Why do I get a AuthenticationException when invoking the EJB remotely on TomEE? 加载使用ASM创建的类文件时,为什么会出现魔术值错误? - Why do I get a magic value error when loading a class file created with ASM? 我在尝试使用 main() 方法运行 class 时收到 IllegalAccessError - I am receiving an IllegalAccessError when trying to run the class with the main() method in it 为什么在扩展类时会出现ClassCastException? - Why do I get ClassCastException when I extend a class? 为什么在使用Class.forName(…)时会收到ClassNotFoundException? - Why do I get a ClassNotFoundException when using Class.forName(…)? 声明库包的类时出现IllegalAccessError - IllegalAccessError when declaring a class for a library package 在JMock中访问包私有类时出现IllegalAccessError - IllegalAccessError when accessing package private class in JMock 使用Jena Ontology API加载.owl文件时,为什么会收到log4j:WARN消息? - Why do I get a log4j:WARN message when loading a .owl file with Jena Ontology API? 延迟加载多对多实体时,为什么会收到N + 1个选择查询 - Why do I get N+1 select queries when lazy loading many-to-many entities 为什么在Windows上调用MinGW编译的函数(不加载库)时会出现UnsatisfiedLinkError? - Why do I get UnsatisfiedLinkError when calling a MinGW-compiled function (not loading the library) on Windows?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM