gpt4 book ai didi

类加载器上的 java.lang.ClassNotFoundException

转载 作者:行者123 更新时间:2023-12-01 10:59:54 25 4
gpt4 key购买 nike

这段代码的想法是接收一些java文件,这些文件扩展了我在服务器上的另一个java文件。

此时我忽略了明显的安全问题,但代码如下:

private boolean isTheFileIamLookingFor(String name,String path,String nameOfFatherClass) {
System.out.println("NAME IS "+name); //prints correct class name
System.out.println("PATH IS "+path); //prints correct path
if(Files.getFileExtension(path).equals("class"))
{
try {
ClassLoader classLoader = this.getClass().getClassLoader();
Class<?> loadedMyClass = classLoader.loadClass(String.format("%s.class", name));
Class<?> c[] = loadedMyClass.getInterfaces();
if(Arrays.asList(c).contains(Class.forName(nameOfFatherClass)))
{
return true;
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
return false;

}

但我收到错误

java.lang.ClassNotFoundException: MyClass.class

但事实是该文件就在那里。关于为什么找不到该类(class)有什么想法吗?

cmd log

最佳答案

导入java.util.Arrays;导入 org.apache.commons.io.FilenameUtils;

公共(public)类mytest{

public static void main(String[] args) {
mytest m = new mytest();
System.out.println(m.isTheFileIamLookingFor("ArrayList", "java.util.",
"List"));
}

public boolean isTheFileIamLookingFor(String name, String path,
String nameOfFatherClass) {
System.out.println("NAME IS " + name); // prints correct class name
System.out.println("PATH IS " + path); // prints correct path
if (!FilenameUtils.getExtension(path).equals("class"))

{
try {
ClassLoader classLoader = this.getClass().getClassLoader();
System.out.println(String.format("%s.class", name));
Class<?> loadedMyClass = classLoader.loadClass(path + name);
Class<?> c[] = loadedMyClass.getInterfaces();
if (Arrays.asList(c).contains(
Class.forName(path + nameOfFatherClass))) {
return true;
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
return false;

}

}

关于类加载器上的 java.lang.ClassNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33426774/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com