[英]Java Class signature and dynamic loading
When will two classes considered same? 什么时候将两个班级视为相同? Like, is there something acting as the signature of a class? 就像,有什么东西可以作为班级的标志吗? If so, what counts in the signature, package info, class name, etc? 如果是这样,签名,程序包信息,类名等中要考虑什么? I'm asking this because I need to dynamically load a class and I always got a ClassNotFoundException
我问这个问题是因为我需要动态加载一个类,并且总是收到ClassNotFoundException
A bit more detail: I'm using Eclipse. 更详细一点:我正在使用Eclipse。 I have an abstract class Panel
in my package com.example.project.sub1
. 我的package com.example.project.sub1
有一个abstract class Panel
。 And a class Test
in package com.example.project.sub2
, which will call package com.example.project.sub2
有一个class Test
,它将调用
ClassLoader loader = new URLClassLoader( new URL[]{new URL("file://" + path)}); /*the path is specified runtime and can be in a different directory other than working directory. It's the path to the parent directory of the class file I need to load. */ Class<Panel> panelClass = (Class<Panel>)loader.loadClass(className); //class name is runtime specified.
That compiles fine. 这样编译就可以了。 Then I copied all the stuff in Panel.java
in a new directory and created a class MyPanel extends Panel
along with Panel.java
. 然后,我将Panel.java
中的所有内容复制到新目录中,并创建了一个class MyPanel extends Panel
与Panel.java
一起class MyPanel extends Panel
。 That compiles fine too, but when I specify the path to my new MyPanel.class
, I always got a ClassNotFoundException
. 编译也很好,但是当我指定新MyPanel.class
的路径时,总是得到ClassNotFoundException
。 Any idea where I'm wrong? 知道我错了吗? Thanks. 谢谢。
EDIT: The stack trace: 编辑:堆栈跟踪:
java.lang.ClassNotFoundException: MyPanel at java.net.URLClassLoader$1.run(URLClassLoader.java:202) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:190) at java.lang.ClassLoader.loadClass(ClassLoader.java:306) at java.lang.ClassLoader.loadClass(ClassLoader.java:247) at com.example.project.sub2 (Test.java:111) at java.lang.Thread.run(Thread.java:680)
java.lang.ClassNotFoundException: MyPanel java.lang.ClassNotFoundException:MyPanel
The class is not called "MyPanel", it is "com.example.project.sub1.MyPanel". 该类不称为“ MyPanel”,而是“ com.example.project.sub1.MyPanel”。
It's the path to the parent directory of the class file I need to load. 这是我需要加载的类文件的父目录的路径。
Yes, that won't work. 是的,那行不通。 It needs to be the directory at the root of the package hierarchy, so not "/some/path/classes/com/example/project/sub1/", but "/some/path/classes" 它必须是包层次结构根目录下的目录,因此不是“ / some / path / classes / com / example / project / sub1 /”,而是“ / some / path / classes”
If you want to dynamically load the class com.example.project.sub1.Panel
from a file URL, then this URL should refer to the directory containing the directory com
. 如果要从文件URL动态加载类com.example.project.sub1.Panel
,则该URL应该引用包含目录com
的目录。 From there, the ClassLoader will look for the class in a subdirectory path matching the package path. 从那里,ClassLoader将在与包路径匹配的子目录路径中查找类。 And the class name to pass is the fully qualified name: com.example.project.sub1.Panel
. 要传递的类名称是完全限定名称: com.example.project.sub1.Panel
。
Also, loding the class MyPanel will return a Class<MyPanel>
, not a Class<Panel>
. 同样,放置类MyPanel将返回Class<MyPanel>
,而不是Class<Panel>
。 You should use Class<? extends Panel>
您应该使用Class<? extends Panel>
Class<? extends Panel>
as the type of the variable. Class<? extends Panel>
为变量的类型。
I'm not sure why you're dynamically loading classes though. 我不确定为什么要动态加载类。 The actual problem you're trying to solve is probably solvable in another way. 您尝试解决的实际问题可能可以通过其他方式解决。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.