我正在尝试做并且一直在研究如何做很多天是有一个母罐子,它可以通过一个文件夹查看并动态加载所有罐子(不需要知道它们),这样我才能工作插件内需要一个名为plugin
. 我目前有一个可以启动插件的工作类加载器,但我无法读取plugin
文件。
这是我当前的代码:
加载类
Load(JMenuBar Bar) {
ClassLoader currentThreadClassLoader = Thread.currentThread().getContextClassLoader();
ClazzL = new URLClassLoader(new URL[]{new File("/home/grant/plugins/MenuPlugin.jar").toURL()}, currentThreadClassLoader);
Thread.currentThread().setContextClassLoader(ClazzL);
in = new BufferedReader(new InputStreamReader(ClazzL.getClass().getClassLoader().getResourceAsStream("/menuplugin/plugin")));
System.out.println("in: " + in.readLine());
Class c = ClazzL.loadClass("menuplugin.Main");//this works and loads properly
//it is just that it needs to
//load from `plugin` using
//in.readLine();
API obj = (API) c.newInstance();
obj.run(Bar);
}
错误是(第 44 行:in = new Buffered....)
Exception in thread "main" java.lang.NullPointerException
at menuaddon.Load.<init>(Load.java:42)
at menuaddon.Menuaddon.main(Menuaddon.java:39)
Java Result: 1
非常感谢您的帮助
完成的工作代码是:
ClassLoader currentThreadClassLoader = Thread.currentThread().getContextClassLoader();
ClazzL = new URLClassLoader(new URL[]{new File("/home/grant/plugins/MenuPlugin.jar").toURL()}, currentThreadClassLoader);
Thread.currentThread().setContextClassLoader(ClazzL);
in = new BufferedReader(new InputStreamReader(ClazzL.getResourceAsStream("plugin")));
classpath = in.readLine();
Class c = ClazzL.loadClass(classpath);
API obj = (API) c.newInstance();
obj.run(Bar);