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.
解决方案
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.