简体   繁体   English

如何从另一个.jar中的类调用方法?

[英]How to call a method from a class in another .jar?

I have .jar-file, called Plugin.jar which contains a single class, plugin.Plugin , with the method public ArrayList<String> getList() . 我有.jar文件,名为Plugin.jar ,它包含一个类plugin.Plugin ,方法为public ArrayList<String> getList()

I am trying to call that method from my main program, which is a completely different NetBeans project. 我试图从我的主程序调用该方法,这是一个完全不同的NetBeans项目。

However, I get a java.lang.IllegalArgumentException: object is not an instance of declaring class on the last line before the catch clause. 但是,我得到一个java.lang.IllegalArgumentException: object is not an instance of declaring classcatch子句之前的最后一行java.lang.IllegalArgumentException: object is not an instance of declaring class

The main program (the one that is calling getList() in the other .jar) looks like this: getList()在另一个.jar中调用getList()的程序)如下所示:

public class PluginMain {

    public static void main(String[] args) {
        try {
            File file = new File("plugins/Plugin.jar");
            URL url = file.toURI().toURL();
            URL[] urls = new URL[]{url};
            ClassLoader cl = new URLClassLoader(urls);

            Class cls = cl.loadClass("plugin.Plugin");
            Method method = cls.getDeclaredMethod("getList");
            ArrayList<String> invoke = (ArrayList<String>) method.invoke(cls);
        } catch (MalformedURLException | ClassNotFoundException | NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
            Logger.getLogger(PluginMain.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

The plugin.Plugin class from the Plugin.jar looks like this: plugin.Plugin从类Plugin.jar看起来是这样的:

public class Plugin {

    public Plugin() {
    }

    public ArrayList<String> getList(){
        ArrayList<String> list = new ArrayList();
        list.add("String1");
        list.add("String2");
        list.add("String3");
        return list;
    }
}

This is my first time attempting anything like this and I'm truly lost, as to what I should do to fix the exception. 这是我第一次尝试这样的事情,我真的迷失了,我应该怎么做才能解决这个异常。 Any help will be appreciated. 任何帮助将不胜感激。

The Method#invoke(Object, Object...) method expects an instance of the class to which the method belongs. Method#invoke(Object, Object...)方法需要该方法所属的类的实例。

You have a few choices with reflection. 你有一些反思选择。 If your class has a no-args constructor (as it does), you can use 如果你的类有一个no-args构造函数(就像它一样),你可以使用

Object instance = cls.newInstance();

and then pass that 然后传递那个

ArrayList<String> invoke = (ArrayList<String>) method.invoke(instance);

If your class doesn't have a no-args constructor, you'll need to choose from the available constructors with 如果您的类没有no-args构造函数,则需要从可用的构造函数中进行选择

Constructor[] constructors = cls.getConstructors();

I am trying to call that method from my main program, which is a completely different NetBeans project. 我试图从我的主程序调用该方法,这是一个完全不同的NetBeans项目。

Note that as long as the class files are on the class path, you can very much just do 请注意,只要类文件位于类路径上,您就可以做到

Plugin plugin = new Plugin();
...

the fact that it's in a different project has nothing to do with how you run a java program. 事实上,它在一个不同的项目中与你运行java程序的方式无关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM