簡體   English   中英

使用 NIO 加載外部 JAR 中的所有類

[英]Loading all classes in an external JAR using NIO

我有一個包含一些 JARs 的插件文件夾,我想加載它們的所有 *.class。 我看到很多關於JarFileZipEntry的答案。 但是,我想使用 java.nio package 而不是 io 來執行此操作。

我可以做什么:

  1. 使用Files.list(Path)
  2. 加載 a.class 在 JAR 中,知道它在所述 JAR 中的確切位置。

我還不能做什么:

  1. 使用 NIO 列出 JAR/ZIP 中的文件。

最終,我想要:

  1. 遍歷幾個JARs中的每個class並加載它們來尋找一種插件系統的注解

我應該補充一點,我不需要使用像 Guava 或 Apache 這樣的 API 的工作解決方案,但我只想知道它是如何完成的。

我當前的代碼:

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
}

我想避免這樣的 java.io 代碼: How to load Classes at runtime from a folder or JAR?

我偶然發現了 zip 文件夾及其子文件夾中的文件列表 我最初錯過了這種方法的是不使用文件夾中的 jar 路徑調用Files.walkFileTree(Path, FileVisitor) ,而是使用創建的文件系統的根路徑(使用FileSystem FileSystem.getPath("/") )。 這非常適合我。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM