External plugin loading setup

Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
main
sigonasr2, Sig, Sigo 2 years ago committed by GitHub
parent 392b731234
commit bcaa025af2
  1. BIN
      bin/TestPlugin.jar
  2. 1
      manifest
  3. BIN
      plugins/TestPlugin.jar
  4. 4
      plugins/TestPlugin.java
  5. 19
      src/sig/PluginLoader.java

Binary file not shown.

@ -0,0 +1 @@
Main-Class: TestPlugin

Binary file not shown.

@ -1,3 +1,5 @@
public class TestPlugin {
public TestPlugin() {
System.out.println("Loaded me!");
}
}

@ -1,6 +1,10 @@
package sig;
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.Path;
@ -8,9 +12,22 @@ public class PluginLoader {
public static void LoadPlugins() {
Path p = Path.of("..","plugins");
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) {
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…
Cancel
Save