Java 插件加载程序中的ClassCastException
我目前在Java方面遇到了另一个奇怪的问题 我的目标是制作一个小型插件系统,为我的一个应用程序提供API。我成功地做到了这一点,它在Windows中运行得非常好。 但是当我尝试在Linux(Debian 7)中使用相同的插件运行相同的应用程序时,它抛出了一个ClassCastException 这是加载程序:Java 插件加载程序中的ClassCastException,java,classcastexception,urlclassloader,Java,Classcastexception,Urlclassloader,我目前在Java方面遇到了另一个奇怪的问题 我的目标是制作一个小型插件系统,为我的一个应用程序提供API。我成功地做到了这一点,它在Windows中运行得非常好。 但是当我尝试在Linux(Debian 7)中使用相同的插件运行相同的应用程序时,它抛出了一个ClassCastException 这是加载程序: public ArrayList<ServerPlugin> loadPlugins() throws ServerException { ArrayList<S
public ArrayList<ServerPlugin> loadPlugins() throws ServerException
{
ArrayList<ServerPlugin> plugins = new ArrayList<ServerPlugin>();
File pluginDir = new File("plugins");
IMServer.getServer().getSystemLogger().log(Logger.INFO, "Plugin Folder: " + pluginDir.getAbsolutePath());
File[] jars = pluginDir.listFiles(new FileFilter()
{
public boolean accept(File pathname)
{
return pathname.getName().endsWith(".jar");
}
});
try
{
for (File f : jars)
{
URLClassLoader loader = URLClassLoader.newInstance(new URL[] { f.toURI().toURL() });
ResourceBundle props = ResourceBundle.getBundle("plugin", Locale.getDefault(), loader);
final String pluginClassName = props.getString("MainClass");
final String pluginName = props.getString("PluginName");
IMServer.getServer().getSystemLogger().log(Logger.INFO, "Enabling " + pluginName);
Object pluginObject = loader.loadClass(pluginClassName).newInstance();
if (pluginObject instanceof ServerPlugin)
{
ServerPlugin plugin = (ServerPlugin) pluginObject;
plugin.onLoad();
plugins.add(plugin);
}
}
}
catch (Exception ex)
{
IMServer.getServer().getSystemLogger().log(Logger.ERROR, "Unknown error in plugin system: \n");
ex.printStackTrace();
}
return plugins;
}
当然,我有一个包声明和导入,我只是把它们剪掉,使它更可读
现在,这是plugin.properties文件,用于加载插件:
public class Plugin implements ServerPlugin {
@Override
public CommandList getCommandList() {
CommandList commands = new CommandList();
commands.add("SEND");
return commands;
}
@Override
public void onCommand(String command, String[] args, User sender) throws ServerException {
if (args[1].equalsIgnoreCase("!test"))
{
String message = "Server:Testplugin";
message += ";" + timestamp();
try
{
sender.getOutputStream().write(message + "\n");
sender.getOutputStream().flush();
}
catch (IOException ex)
{
throw new CommunicationException(ex.getMessage());
}
}
}
private String timestamp()
{
SimpleDateFormat format = new SimpleDateFormat("MM-dd/HH-mm-ss");
Date currentTime = new Date(System.currentTimeMillis());
return format.format(currentTime);
}
@Override
public void onLoad() {
System.out.println("Hello from test plugin!");
}
@Override
public void onUnload() {
System.out.println("Goodbye from test plugin :-(");
}
}
MainClass=me.test.Plugin
PluginName=Testplugin
在我构建thingy实例之前引发异常的行是serverpluginplugin=(ServerPlugin)pluginObject代码>和堆栈跟踪如下所示:
java.lang.ClassCastException: me.test.Plugin cannot be cast to net.dreamcode.im.server.plugins.ServerPlugin
at net.dreamcode.im.server.plugins.PluginManager.loadPlugins(PluginManager.java:68)
at net.dreamcode.im.server.IMServer.startServer(IMServer.java:165)
at net.dreamcode.im.server.IMServer.main(IMServer.java:70)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
我想知道为什么要调用Eclipse的一部分。也许这就是我问题的原因
提前谢谢你的帮助 如果您是从eclipse导出的带有打包库的jar运行,则会发生JarrLoader调用。感谢您的快速回复。这是我的问题吗?如果是这样,我会尝试在没有库的情况下重新打包。