简体   繁体   English

如何在java中运行.jar文件

[英]How can i run a .jar file in java

I'm making an update function for my project, it's working great, until i want it to restart, basically I download the new file and replace it with the old one, and then i want to run it again, now for some reason it doesn't wna run, and i don't get any error... 我正在为我的项目制作更新功能,它工作得很好,直到我希望它重新启动,基本上我下载新文件并用旧文件替换它,然后我想再次运行它,现在由于某种原因它不会运行,我没有任何错误...

Here is the complete update class: http://dl.dropbox.com/u/38414202/Update.txt 这是完整的更新类: http//dl.dropbox.com/u/38414202/Update.txt

Here is the method i'm using to run my .jar file: 这是我用来运行.jar文件的方法:

 String currDir = new File("(CoN).jar").getAbsolutePath();
 Process runManager = Runtime.getRuntime().exec("java -jar " + currDir);

It's not clear to me, why do you need to run the jar with a call to exec() . 我不清楚为什么你需要通过调用exec()来运行jar。 Given that you need to run the code in the .jar file from a Java program, you could simply run the main() method as defined in the jar's manifest, and capture its output - wherever that is. 鉴于您需要从Java程序运行.jar文件中的代码,您可以简单地运行jar清单中定义的main()方法,并捕获其输出 - 无论在哪里。

Using exec() is OK when you need to call a program from the underlying operating system, but there are easier ways to do this if both the caller and the callee are Java programs. 当您需要从底层操作系统调用程序时,使用exec()是正常的,但如果调用者和被调用者都是Java程序,则有更简单的方法可以执行此操作。

Now, if your jar is gonna change dynamically and you need to update your program according to a new jar, there are mechanisms for reloading its contents, for instance take a look ath this other post . 现在,如果您的jar将动态更改并且您需要根据新jar更新您的程序,则有重新加载其内容的机制,例如,看看其他帖子

The JavaDocs for the Process class specifically point out that if you don't capture the output stream of the Process and promptly read it that the process could halt. Process类的JavaDocs特别指出,如果您没有捕获Process的输出流并立即读取它,那么该进程可能会停止。 If this is the case, then you wouldn't see the process that you started run. 如果是这种情况,那么您将看不到您开始运行的进程。

I think you have to capture the stream like this : 我认为你必须像这样捕获stream

BufferedReader stdInput = new BufferedReader(new InputStreamReader(runManager.getInputStream()),8*1024);

BufferedReader stdError = new BufferedReader(new InputStreamReader(runManager.getErrorStream()));

// read the output from the command

String s = null;
System.out.println("Here is the standard output of the command:\n");
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
    }

The exec function doesn't automatically lookup into the PATH to start a process, so you have to pass the complete path for the java binary. exec函数不会自动查找到PATH以启动进程,因此您必须传递java二进制文件的完整路径。

You can do that by using the java.home system property, see this answer: ProcessBuilder - Start another process / JVM - HowTo? 您可以使用java.home系统属性来做到这一点,请参阅以下答案: ProcessBuilder - 启动另一个进程/ JVM - HowTo?

No one here seemed to help me, so I went to ask my friend and I had it almost right. 这里似乎没有人帮助我,所以我去问我的朋友,我几乎是对的。 It abiously required the string to be an array. 它非常需要字符串作为数组。

solution: 解:

String[] cmd = {"java", "-jar", currDir};
try {
  Runtime.getRuntime().exec(cmd);
} catch (IOException e1) {
  e1.printStackTrace();
}

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

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