从其他文件夹加载Jar文件 [英] Loading Jar File From Different Folder

查看:54
本文介绍了从其他文件夹加载Jar文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在制作Minecraft Mod Loader.

I am currently making a Minecraft Mod Loader.

package spideyzac;

import java.util.concurrent.CopyOnWriteArrayList;

import org.lwjgl.opengl.Display;

import spideyzac.Module;

public class Client {
    
    public static String name = "Pizza Mod Loader", version = "1";
    public static CopyOnWriteArrayList<Module> modules = new CopyOnWriteArrayList<Module>();
    
    public static void startup() {
        Display.setTitle(name + " v" + version);
    }
    
    public static void onKey(int key) {
        for (Module m : modules) {
            if (m.keyCode == key) {
                m.toggle();
            }
        }
    }
    
    public static void update() {
        for (Module m : modules) {
            if (m.isEnabled()) {
                m.ifEnabled();
            } else {
                m.ifNotEnabled();
            }
        }
    }
    
}

正如您在上面看到的,我有一个名为Client的类.Minecraft游戏启动时,将调用启动.现在,我有一个名为Mods的文件夹,调用启动时,我需要将mods从mods文件夹加载到名为modules的ArrayList中.更深入地讲,每个Mod都有一个Main类,它继承了这个Module类

As you can see above I have a class called Client. Startup is called when the Minecraft game launches. Now I have a folder named Mods and when startup is called I need to load the mods from the mods folder into the ArrayList named modules. More in depth, each Mod will be have a Main class which inherits this Module class

package spideyzac;

import org.lwjgl.input.Keyboard;

import net.minecraft.client.Minecraft;

public class Module {

    public static int keyCode;
    public static String name;
    public static boolean enabled;
    public Minecraft mc = Minecraft.getMinecraft();
    
    Module (int keyCode, String name) {
        this.keyCode = keyCode;
        this.name = name;
    }
    
    public void onEnable() {
        
    }
    
    public void onDisable() {
        
    }
    
    public void toggle() {
        enabled = !enabled;
        if (enabled) {
            onEnable();
        } else {
            onDisable();
        }
    }
    
    public void ifEnabled() {
        
    }
    
    public void ifNotEnabled() {
        
    }
    
    
    public static boolean isEnabled() {
        return enabled;
    }
    
    public static int getKeyCode() {
        return keyCode;
    }
    
    public static boolean checkKey(int key) {
        return Keyboard.isKeyDown(key);
    }
    
}

因此,在调用启动时,我需要遍历mods文件夹中的每个Mod,并将该模块的Main类的新实例添加到ArrayList模块中.

So when startup is called, I need to go through each Mod in the mods folder and add a new instance of the module's Main class to the ArrayList modules.

推荐答案

为此有一个名为URLClassLoader的类.

There is a class named URLClassLoader for this.

顾名思义,它可以从URL加载类:

As the name says, it can load classes from URLs:

File[] files=new File("Mods").listFiles();
URL[] urls=new URL[files.length];
for(int i=0;i<files.length;i++){
    urls[i]=files[i].toURI().toURL();
}
URLClassLoader modLoader=new URLClassLoader(urls);

一行:

URLClassLoader modLoader=new URLClassLoader(Stream.of(new File("Mods").list()).map(file->file.toURI().toURL()).toArray(URL[]::new));

您还可以为每个JAR创建一个ClassLoader.

You can also create one ClassLoader per JAR.

File[] files=new File("Mods").listFiles();
for(int i=0;i<files.length;i++){
    URL url=files[i].toURI().toURL();
    URLClassLoader modLoader=new URLClassLoader(url);
}

如果知道软件包名称,则可以加载类:

You can then load classes if you know the package name:

Class<?> cl=modLoader.loadClass("fully.qualified.name");
Object o=cl.getDeclaredConstructor().newInstance();
if(o instanceof Module){
    modules.add((Module)o);
}

如果您不知道类名,则可以使用名为Reflections的库来获取 Class 对象.

If you don't know the class names, you can use a library called Reflections in order to get the Class objects.

这篇关于从其他文件夹加载Jar文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆