1

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.

5
  • 1
    try ProcessBuilder instead Commented Jan 5, 2015 at 23:51
  • Also you don't need to "double" forward-slashes in a string, only backslashes. Commented Jan 5, 2015 at 23:52
  • Have you tried running the jar from CMD or by creating .bat file? Commented Jan 5, 2015 at 23:53
  • @OndrejTokar Making it target a bat instead of a jar worked. Not sure why, but thanks! Commented Jan 5, 2015 at 23:58
  • I am not sure but I have faced very similar problem. The issue might be path to the file. Maybe you could also try placing the jar locally. Commented Jan 6, 2015 at 0:03

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.