执行外部JAR

时间:2013-04-26 22:05:42

标签: java jar external

我为我的游戏编写了一个启动器,当给出有效的用户名和密码时,如果该文件尚不存在或者有更新,则会从网站下载JAR文件。登录系统和文件下载工作,但如何运行下载的JAR文件?

我尝试了Runtime.getRuntime().exec("java -jar " + file.getAbsolutePath());,但无济于事。

感谢您的帮助!


downloading = new JLabel("Download AudioRPG executable from server. This may take up to a minute.");
                            downloading.setHorizontalAlignment(SwingConstants.CENTER);

                            Thread th = new Thread(new Runnable() {
                                public void run() {
                                    remove(username);
                                    remove(password);
                                    remove(submit);
                                    remove(remember);
                                    add(downloading, BorderLayout.CENTER);
                                    pack();

                                    try {
                                        FTPClient client = new FTPClient();

                                        client.connect("audiorpg.net");
                                        client.login("audiorpg", "mcpogotime1");
                                        client.changeDirectory("files");
                                        client.download("AudioRPG.jar", exe);
                                        client.disconnect(true);
                                        downloading.setText("Done! Launching AudioRPG...");
                                    }
                                    catch (Exception e) { e.printStackTrace(); }
                                }
                            });

                            th.start();

                            startExternalJAR(getClass(), th, exe);

private static void startExternalJAR(Class<?> c, Thread th, File exe) {
        if (!th.isAlive()) {
            try {
                final String mainClass;
                final JarFile jarFile = new JarFile(exe);
                try {
                    final Manifest manifest = jarFile.getManifest();
                    mainClass = manifest.getMainAttributes().getValue("Main-Class");
                } finally {
                    jarFile.close();
                }
                final URLClassLoader child = new URLClassLoader(new URL[]{exe.toURI().toURL()}, c.getClassLoader());
                final Class<?> classToLoad = Class.forName(mainClass, true, child);
                final Method method = classToLoad.getDeclaredMethod("main", String[].class);
                final Object[] arguments = {new String[0]};
                method.invoke(null, arguments);
            }
            catch (Exception ex) { ex.printStackTrace(); }
        }
        else {
            try { Thread.sleep(1000); }
            catch (Exception e) { }
            startExternalJAR(c, th, exe);
        }
    }

这是我现在尝试使用的代码,但它无效。 @Boris the Spider有什么提示吗?

2 个答案:

答案 0 :(得分:4)

不要像命令一样执行jar。使用类加载器从jar中加载所需的类,然后实例化它。

http://docs.oracle.com/javase/tutorial/deployment/jar/jarclassloader.html

  1. 通过构建new JarClassLoader("url goes here")
  2. 来加载jar
  3. 在JarClassLoader上调用.invokeClass("MyMainClassName", new String[] { "Args", "Go", "Here" })

答案 1 :(得分:0)

您应该使用URLClassLoader加载jar,然后拨打main

final String mainClass;
final JarFile jarFile = new JarFile(file);
try {
    final Manifest manifest = jarFile.getManifest();
    mainClass = manifest.getMainAttributes().getValue("Main-Class");
} finally {
    jarFile.close();
}
final URLClassLoader child = new URLClassLoader(new URL[]{file.toURI().toURL()}, this.getClass().getClassLoader());
final Class classToLoad = Class.forName(mainClass, true, child);
final Method method = classToLoad.getDeclaredMethod("main", String[].class);
final Object[] arguments = {new String[0]};
method.invoke(null, arguments);

取自this SO answer