[英]Loading all classes in an external JAR using NIO
I have a plugin folder that contains some JARs, and I want to load all of their *.class.我有一个包含一些 JARs 的插件文件夹,我想加载它们的所有 *.class。 I see a lot of answers with JarFile
or ZipEntry
stuff.我看到很多关于JarFile
或ZipEntry
的答案。 However, I would like to do this using java.nio package and not io.但是,我想使用 java.nio package 而不是 io 来执行此操作。
What I can do:我可以做什么:
Files.list(Path)
使用Files.list(Path)
What I cannot do yet:我还不能做什么:
Ultimately, I want:最终,我想要:
I should add that I do not need a working solution using APIs like Guava or Apache but I just want to know how it's done.我应该补充一点,我不需要使用像 Guava 或 Apache 这样的 API 的工作解决方案,但我只想知道它是如何完成的。
My current code:我当前的代码:
import java.io.IOException;
import java.nio.file.*;
import java.security.SecureClassLoader;
import java.util.*;
public class PluginManager {
public final Path pluginFolder;
public PluginManager(Path pluginFolder) {
this.pluginFolder = pluginFolder;
}
public void load() {
final var pluginClassLoader = new SecureClassLoader(getClass().getClassLoader()){
FileSystem jar;
@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
String pluginClassName = jar.getSeparator() + name.replaceAll("\\.", jar.getSeparator()) + ".class";
try{
byte[] bytes = Files.readAllBytes(jar.getPath(pluginClassName));
return defineClass(name, bytes, 0, bytes.length);
}catch(IOException e){
e.printStackTrace();
}
throw new ClassNotFoundException("Failed to found "+name);
}
};
try(Stream<Path> files = Files.list(pluginFolder)){
files.filter((file) -> !Files.isDirectory(file) &&
(file.toString().endsWith(".jar") || file.toString().endsWith(".zip")))
.forEach(jar -> {
try(FileSystem fileSystem = FileSystems.newFileSystem(jar)){
pluginClassLoader.jar = fileSystem;
loadPlugin(fileSystem, pluginClassLoader);
}catch(IOException | ClassNotFoundException e){
e.printStackTrace();
}
});
}catch(IOException e){
e.printStackTrace();
throw new IllegalStateException("Failed to scan plugin folder. Cannot continue.");
}
}
private void loadPlugin(FileSystem jar, SecureClassLoader classLoader) throws IOException, ClassNotFoundException{
String className = ... // here, we retrieve the plugin class name from a manifest file using FileSystem.getPath().
// what I want is to get rid of that manifest and to load and test every class.
Class<?> clazz = classLoader.loadClass(className);
// do stuff with loaded class
}
I want to avoid java.io code like this: How to load Classes at runtime from a folder or JAR?我想避免这样的 java.io 代码: How to load Classes at runtime from a folder or JAR?
I stumbled across this List of files inside a zip folder and its subfolder .我偶然发现了 zip 文件夹及其子文件夹中的文件列表。 What I initially missed with this approach was not to call Files.walkFileTree(Path, FileVisitor)
with the jar path from the folder, but rather use the root path of the FileSystem
created (using FileSystem.getPath("/")
).我最初错过了这种方法的是不使用文件夹中的 jar 路径调用Files.walkFileTree(Path, FileVisitor)
,而是使用创建的文件系统的根路径(使用FileSystem
FileSystem.getPath("/")
)。 This works perfectly for me.这非常适合我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.