我希望能够浏览,虽然一个文件夹中的所有文件夹来加载这些文件夹中的类通过浏览文件夹加载类
我当前的代码不检查所有软件包。它只会在没有打包的情况下加载类。
@SuppressWarnings({"rawtypes", "unchecked"})
public Class[] getClassesFromFolder() {
File folder = getFolder();
String thePath = folder.getPath();
ArrayList<Class<Script>> classes = new ArrayList<Class<Script>>();
try {
URL[] path = {new URL("file://" + thePath + "/Scripts/")};
File scriptFolder = new File(getFolder().getPath() + "/Scripts");
URLClassLoader cl = new URLClassLoader(path);
for (String script : scriptFolder.list()) {
if (script.contains(".class") && !script.contains("$")) {
String truePath = script.replace(".class", "");
try {
Class<?> scriptClass = (Class<?>) cl
.loadClass(truePath);
classes.add((Class<Script>) scriptClass);
} catch (Exception e) {
e.printStackTrace();
}
}
}
cl.close();
} catch (Exception e) {
e.printStackTrace();
}
return classes.toArray(new Class<?>[classes.size()]);
}
什么具体是你的问题?不要期望其他人从您的代码中扣除可能想到的内容;以及为什么这不起作用。 – GhostCat