我目前正在遇到Java的另一个奇怪问题。
我的目标是制作一个小插件系统,为我的某个应用程序提供API。我设法做了这样的事情,它在Windows中运行得很好。 但是当我尝试在Linux(Debian 7)中使用相同的插件运行相同的应用程序时,它会抛出ClassCastException。
这是装载机:
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;
}
正如您所看到的,我尝试通过构建一个instanceof check来防止异常。现在我没有得到异常,但插件也没有加载(有点明显)。
以下是插件的代码:
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 :-(");
}
}
当然,我有一个包装声明和导入,我只是将它们剪掉以使其更具可读性。
现在,这是plugin.properties文件,用于加载插件:
MainClass=me.test.Plugin
PluginName=Testplugin
在我构建的instanceof之前抛出异常的行是ServerPlugin plugin = (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的一部分。也许这是我的问题的原因?
提前感谢您的帮助!