我正在一个大学的Java项目中工作,该项目要求我实现一个简单的插件体系结构。项目A中的一个主要应用程序必须从另一个项目B中的指定目录中加载插件。在对该主题进行了一些研究(包括stackoverflow)之后,我决定使用URLClassLoader来加载那些我可能随后会实例化的类。项目B引用项目A,并且所有插件都扩展了一个通用的插件类(也可以是一个接口,但不应有任何区别)。到目前为止,这是我得到的:
我尝试加载的插件类的源代码:
package plugin;
import de.uks.student.pluginpattern.model.EditorPlugin;
public class Circle extends EditorPlugin
{
@Override
public void init()
{
}
}
以及应该加载该类的代码:
public void init(String[] args)
{
editorPane = new EditorPane().withWidth(500).withHeight(500);
toolBar = new ToolBar();
plugins = new EditorPluginSet();
// load plugins
File pluginDir = new File(PLUGIN_PATH);
if (!pluginDir.exists())
{
System.err.println("Plugin path not found!!");
return;
}
String[] plugins = pluginDir.list();
if (plugins == null)
{
System.err.println("Plugin path points to a file!!");
return;
}
for (String pluginString : plugins)
{
for (String argString : args)
{
if (pluginString.contains(argString))
{
System.out.println("Loading plugin from " + pluginString);
File pluginFile = new File(PLUGIN_PATH + "/");
// as getAbsolutePath embeds the relative path ... /../pluginProject ...
// which may not be processed correctly by the OS (Windows at least),
// we correct the path manually (just to be on the safe side)
String absolutePath = pluginFile.getAbsolutePath();
String[] split = absolutePath.split("\\\\");
List<String> splitsimpleList = Arrays.asList(split);
ArrayList<String> splitList = new ArrayList<>(splitsimpleList);
for (int i = 0; i < splitList.size() - 1; ++i)
{
if (splitList.get(i + 1).equals(".."))
{
splitList.remove(i);
splitList.remove(i);
break;
}
}
StringBuilder b = new StringBuilder();
for (String string : splitList)
{
b.append(string);
b.append("/");
}
File pluginFileForRealThisTime = new File(b.toString());
URL pluginURL = null;
try
{
pluginURL = pluginFileForRealThisTime.toURI().toURL();
} catch (MalformedURLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
URL[] urls = {pluginURL};
ClassLoader parentClassLoader = this.getClass().getClassLoader();
URLClassLoader uLoader = new URLClassLoader(urls, parentClassLoader);
Class<?> pluginClass = null;
try
{
String className = "plugin." + argString;
pluginClass = uLoader.loadClass(className);
} catch (ClassNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
现在,我总是以ClassNotFoundException结尾:
java.lang.ClassNotFoundException:plugin.Circle在
java.net.URLClassLoader $ 1.run(未知源)
java.net.URLClassLoader $ 1.run(未知源)
java.security.AccessController.doPrivileged(本机方法),位于
java.net.URLClassLoader.findClass(未知源),网址为
java.lang.ClassLoader.loadClass(未知源)
java.lang.ClassLoader.loadClass(未知源)
de.uks.student.pluginpattern.model.EditorSystem.init(EditorSystem.java:412)
在
de.uks.student.pluginpattern.EditorSystem.main(EditorSystem.java:13)
我已经检查过了
生成的URL可以通过任何Web浏览器和Windows资源管理器正确解析。
Circle.class已构建,并存在于所用URL引用的目录中
className解析为“ plugin.Circle”,它应该是要使用的正确二进制名称
从plugin.Circle删除继承没有任何区别。
我几乎没有其他尝试方法的想法。我究竟做错了什么?
最佳答案
我必须将ClassLoader指向bin文件夹而不是bin / plugins文件夹