从Java代码执行Jar [英] Execute Jar from Java code

查看:1108
本文介绍了从Java代码执行Jar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码应该运行一个可执行jar,但每当代码执行时都没有任何反应?

I have this code that is supposed to run a executable jar, but whenever the code is executed nothing happens?

      try {
           proc = Runtime.getRuntime().exec("java -jar C://X-Dock//MP3Player.jar");
      } catch (IOException e1) {
           e1.printStackTrace();
      }

如果我手动运行它,JAR工作正常,但是那行代码只是不起作用。我肯定知道代码被调用了。

The JAR works fine if I run it manually, but that line of code just doesn't work. And I know for sure that the code is called.

推荐答案

如果在版本5之后有一个JRE,java提供了一个流程构建器。所以,尝试这样的事情:

If you has a JRE after version 5, java provides a process builder. So, try something like this:

final ProcessBuilder pBuilder = new ProcessBuilder("/java/path", "-jar", "your_jar.jar");
pBuilder.directory(new File("your/working/directory"));
final Process process = pBuilder.start();

/ java / path是java安装的路径,可以用java代替,如果java是在环境变量中。

"/java/path" is path for java installation, can be replaced by java, if java is in the environment variables.

ProcessBuilder

这篇关于从Java代码执行Jar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆