如何在另一个jar文件中调用方法

时间:2015-07-28 00:31:51

标签: java server minecraft

我注意到一个minecraft服务器加载了外部jar文件。我想用它来创建一个小型游戏加载的集线器。我想通过调用一组jar文件中的方法来加载它们。如果不直接执行它或修改类路径,这是否可行。我正在使用以下代码来启动和加载集线器:

EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Window window = new Window();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }); 

现在我将为一个文件夹中的jar文件数运行一个循环。我将这些存储在一个数组中。然后我希望按钮的操作从jar文件

调用加载方法

1 个答案:

答案 0 :(得分:0)

  1. Add the jar to your buildpath of the project, right click the project
  2. Select "Build Path" option
  3. Select "Configure Build Path" and add in the external jar

Once you've configured your buildpath you import the class, create an instance of the class to use whatever method you're looking for. That should be it.

I think you can also do something like this (once it's added to your buildpath), not 100% sure on this one though.

ClassNameFromJarYouWant.MethodYouWant();

That'd be the easiest.

But if you really need to be able to add the JARs without modifying the classpath look up URLClassLoader. Here's a post I found that might help you.