External plugin loading setup
Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
This commit is contained in:
parent
392b731234
commit
bcaa025af2
BIN
bin/TestPlugin.jar
Normal file
BIN
bin/TestPlugin.jar
Normal file
Binary file not shown.
BIN
plugins/TestPlugin.jar
Normal file
BIN
plugins/TestPlugin.jar
Normal file
Binary file not shown.
@ -1,3 +1,5 @@
|
|||||||
public class TestPlugin {
|
public class TestPlugin {
|
||||||
|
public TestPlugin() {
|
||||||
|
System.out.println("Loaded me!");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,10 @@
|
|||||||
package sig;
|
package sig;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.lang.reflect.InvocationTargetException;
|
||||||
|
import java.net.MalformedURLException;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.net.URLClassLoader;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
|
|
||||||
@ -8,9 +12,22 @@ public class PluginLoader {
|
|||||||
public static void LoadPlugins() {
|
public static void LoadPlugins() {
|
||||||
Path p = Path.of("..","plugins");
|
Path p = Path.of("..","plugins");
|
||||||
try {
|
try {
|
||||||
Files.walk(p).filter((f)->Files.isRegularFile(f)).forEach((f)->{System.out.println("Found "+f);});
|
Files.walk(p).filter((f)->Files.isRegularFile(f)&&f.getFileName().toString().contains(".jar")).forEach((f)->{
|
||||||
|
LoadPlugin(f);
|
||||||
|
});
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void LoadPlugin(Path f) {
|
||||||
|
try {
|
||||||
|
System.out.println("Loading "+f);
|
||||||
|
URL l = f.toUri().toURL();
|
||||||
|
URLClassLoader loader = new URLClassLoader(new URL[]{l});
|
||||||
|
Object newPlugin = Class.forName(f.getFileName().toString().replace(".jar",""),true,loader).getDeclaredConstructor().newInstance();
|
||||||
|
} catch (MalformedURLException | InstantiationException | IllegalAccessException | ClassNotFoundException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user