Execute Jar from Java code

拜拜、爱过 提交于 2019-12-24 14:41:58

问题


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();
      }

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.


回答1:


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" is path for java installation, can be replaced by java, if java is in the environment variables.

See more at ProcessBuilder.



来源:https://stackoverflow.com/questions/27789903/execute-jar-from-java-code

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!