2

我为我的游戏编写了一个启动器,当给定一个有效的用户名和密码时,如果该文件不存在或有可用更新,它将从网站下载一个 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 有什么建议吗?

4

2 回答 2

4

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

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

  1. 通过构造一个new JarClassLoader("url goes here").
  2. 调用.invokeClass("MyMainClassName", new String[] { "Args", "Go", "Here" })JarClassLoader。
于 2013-04-26T22:09:04.330 回答
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);

取自这个 SO answer

于 2013-04-26T22:13:25.323 回答