[英]UnmarshalException and ClassNotFoundException: what am I doing wrong?
[英]URLClassLoader keeps throwing ClassNotFoundException. What am I doing wrong?
我正在一个大学的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(本机方法)处)。 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)
我已经检查过了
我几乎没有其他尝试方法的想法。 我究竟做错了什么?
我必须将ClassLoader指向bin文件夹而不是bin / plugins文件夹
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.