Java自定义类加载器中调用findClass时类名的规范
在客户类加载器中,我有一个 findClass 方法,如下所示:
public Class findClass(String className){
byte classByte[];
Class result=null;
result = (Class)classes.get(className);
if(result != null){
return result;
}
try{
return findSystemClass(className);
}catch(Exception e){
}
try{
String classPath =
((String)ClassLoader.getSystemResource(className.replace('.',File.separatorChar)+".class").getFile()).substring(1);
classByte = loadClassData(classPath);
result = defineClass(className,classByte,0,classByte.length,null);
classes.put(className,result);
return result;
}catch(Exception e){
return null;
}
}
如果我要查找的类位于默认包中,如何指定该类的名称。例如,如果类名为 myclass.class
,我如何将此名称传递给此方法。将其称为 findClass("myclass")
或 findClass("myclass.class")
似乎不起作用。
In a customer class loader, I have a method findClass as follows:
public Class findClass(String className){
byte classByte[];
Class result=null;
result = (Class)classes.get(className);
if(result != null){
return result;
}
try{
return findSystemClass(className);
}catch(Exception e){
}
try{
String classPath =
((String)ClassLoader.getSystemResource(className.replace('.',File.separatorChar)+".class").getFile()).substring(1);
classByte = loadClassData(classPath);
result = defineClass(className,classByte,0,classByte.length,null);
classes.put(className,result);
return result;
}catch(Exception e){
return null;
}
}
How do I specify the name of a class that I'm trying to find if the class is in the default package. For example, if the class is named myclass.class
, how do I pass this name to this method. Calling it as findClass("myclass")
or findClass("myclass.class")
doesn't seem to work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试使用完全限定名称:
findClass("mypackage.MyClass")
Try with fully qualified name:
findClass("mypackage.MyClass")
最有可能的是由于您提供的名称有问题,请确保它具有正确的包前缀。
给定像“java.lang.String”这样的字符串的该方法将查找文件“java/lang/String.class”(在unix文件系统上)。您可能需要检查您尝试加载的类的文件夹目录/包名称。
它是用点分隔的包名称,然后是类名称,例如:
或
Most likely it's due to a problem with the name you're giving, ensure it has proper package prefixes.
That method given a string like "java.lang.String" will look for the file "java/lang/String.class" (on a unix file system). You may need to check the folder directory/package name of the class you're trying to load.
It's package name separated by dots and then just the class name, example:
or