[英](JAVA) ClassLoader and use of libraries
美好的一天,我想編寫一個模塊化程序並將插件用作每個模塊中的庫。 通過 URLClassLoader 我已經完成了大部分工作。 一切正常。 但問題是圖書館的使用。 當我想使用插件中的方法時,出現錯誤。
它是這樣工作的:我的目錄/ModuleLoader.jar 加載目錄/ModuleLoader/Module.jar 文件。 一切正常。 問題:Module.jar 沒有找到../ModuleLoader.jar 作為依賴項(所以當我擴展一個類時它無法啟動)......
private static final ConcurrentMap<String, ModuleDescription> cache = Maps.newConcurrentMap();
public void loadModules() {
for (File file : Objects.requireNonNull(Main.getInstance().getDataFolder().listFiles())) {
if (!file.getName().substring(file.getName().lastIndexOf(".")).equalsIgnoreCase(".jar")) {
return;
}
registerModule(file);
URLClassLoader loader = null;
try {
loader = new URLClassLoader(new URL[] { file.toURI().toURL() });
Class<?> jarClass = loader.loadClass(cache.get(file.getName()).main);
Method method = jarClass.getMethod("initialise");
Object instance = jarClass.newInstance();
method.invoke(instance);
} catch (ClassNotFoundException | NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException | MalformedURLException e) {
e.printStackTrace();
} finally {
if (loader != null) {
try {
loader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
public void stopModules() {
for (String module : cache.keySet()) {
File file = new File(Main.getInstance().getDataFolder() + "/" + module);
URLClassLoader loader = null;
try {
loader = new URLClassLoader(new URL[] { file.toURI().toURL() });
Class<?> jarClass = loader.loadClass(cache.get(file.getName()).main);
Method method = jarClass.getMethod("shutdown");
Object instance = jarClass.newInstance();
method.invoke(instance);
} catch (MalformedURLException | ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InvocationTargetException | InstantiationException e) {
e.printStackTrace();
} finally {
if (loader != null) {
try {
loader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
public void registerModule(File file) {
JarFile jarFile = null;
InputStream inputStream = null;
try {
jarFile = new JarFile(file);
JarEntry jarEntry = jarFile.getJarEntry("module.yml");
if (jarEntry == null) {
System.out.println("The file \"" + file.getName() + "\" is missing the \"module.yml\"!");
return;
}
inputStream = jarFile.getInputStream(jarEntry);
Map<String, Map<String, String>> values = new Yaml().load(inputStream);
ModuleDescription description = new ModuleDescription();
if (values.containsKey("name")) {
description.name = "" + values.get("name");
System.out.println("name: " + description.name);
}
if (values.containsKey("main")) {
description.main = "" + values.get("main");
System.out.println("main: " + description.main);
}
if (description.name == null || description.main == null) {
System.out.println("The \"module.yml\" is missing needed information's!");
return;
}
if (values.containsKey("version")) {
description.version = "" + values.get("version");
}
if (values.containsKey("author")) {
description.author = "" + values.get("author");
}
if (values.containsKey("description")) {
description.description = "" + values.get("description");
}
cache.put(file.getName(), description);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (jarFile != null) {
try {
jarFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
我已經認識到了這個問題:我嘗試使用該模塊中的 ClassLoader 加載我的模塊。 但是要讓 ModuleLoader 作為依賴項,我還必須使用 ModuleLoader 中的 ClassLoader ....
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.