0

I want to run a jar file from my java code. I have seen many methods of how to do it but none of them have worked.

try {
    File myFile = new File("C:\\Users\\Shayan pc\\Desktop\\AutomatedGameTesting-master\\AutomatedGameTesting-master\\Mario Side-Scroller(Game)\\mario.jar");
    if(myFile.exists()){
        System.out.println("file found");
    }
    Desktop.getDesktop().open(myFile);
} catch (IOException ex) {

}

This method hasnt worked either. Any help would be appreciated.

  • 1
    are you getting an exception ? Also Desktop.getDesktop().open(myFile); should be inside the if condition – Kakarot Sep 08 '16 at 21:19
  • Try via a CMD command: "java -jar jarLocation" – Reinard Sep 08 '16 at 21:20
  • Possible duplicate of [Execute another jar in a java program](http://stackoverflow.com/questions/1320476/execute-another-jar-in-a-java-program) – acm Sep 08 '16 at 21:20
  • @Kakarot it doesnt even work if i lace it inside the if condition and it doesnt give an exception – Shayan Hafeez Khan Sep 08 '16 at 21:22
  • @acm in that problem the jar file is in the same directory as the code – Shayan Hafeez Khan Sep 08 '16 at 21:25
  • Are you trying to run the jar ? If yes then use : Runtime.getRuntime().exec("java -jar ") – Kakarot Sep 08 '16 at 21:27
  • @ShayanHafeezKhan how is that relevant? – acm Sep 08 '16 at 21:29
  • @Kakarot could you please add the file path to this i might be adding it wrong. The path is: C:\Users\Shayan pc\Desktop\AutomatedGameTesting-master\AutomatedGameTesting-master\Mario Side-Scroller(Game)\mario.jar – Shayan Hafeez Khan Sep 08 '16 at 21:31
  • @acm I tried this but it doesnt work as well. Process proc; try { proc = Runtime.getRuntime().exec("java -jar C:\\Users\\Shayan pc\\Desktop\\AutomatedGameTesting-master\\AutomatedGameTesting-master\\Mario Side-Scroller(Game)\\mario.jar"); InputStream in = proc.getInputStream(); InputStream err = proc.getErrorStream(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } – Shayan Hafeez Khan Sep 08 '16 at 21:36
  • **Never** write an empty catch block. That IOException probably would have told you what was going wrong. – VGR Sep 08 '16 at 21:39
  • @VGR added the printstacktrace line still no errors – Shayan Hafeez Khan Sep 08 '16 at 21:46
  • Does your code print "file found" in the terminal? – acm Sep 08 '16 at 21:50
  • Try `new ProcessBuilder("java", "-jar", "C:\\Users\\Shayan pc\\Desktop\\AutomatedGameTesting-master\\AutomatedGameTesting-master\\Mario Side-Scroller(Game)\\mario.jar").inheritIO().start().waitFor()`. This is not the same as using the obsolete Runtime.exec; supplying separate explicit arguments guarantees the space in the .jar filename isn’t causing an issue, and the use of inheritIO() will guarantee the process is not blocking on stdin/stdout/stderr and may also reveal what, if any, error messages mario.jar is generating. – VGR Sep 08 '16 at 22:04
  • @VGR Error: Unable to access jarfile C:\Users\Shayan pc\Desktop\AutomatedGameTesting-master\AutomatedGameTesti??ng-master\Mario Side-Scroller(Game)\mario.jar – Shayan Hafeez Khan Sep 08 '16 at 22:10
  • That's a different problem. Have you tried running `mario.jar` directly from the terminal? – acm Sep 09 '16 at 05:24
  • Those question marks indicate control characters. They are not in your original code as it appears in your question, but they are present in the comment you directed at acm yesterday. I copied the .jar file’s path directly from that comment, and in so doing I unwittingly copied those invisible control characters. You, in turn, copied it into your code. The actual path is `"AutomatedGameTesti\u200c\u200bng-master\\Mario Side-Scroller(Game)\\mario.jar"` (note the **\u200c\u200b**), which of course doesn’t exist. Solution: Type the entire path manually instead of copy and pasting it. – VGR Sep 09 '16 at 13:39

2 Answers2

0
Process p = Runtime.getRuntime()
                              .exec("cmd /c start cmd.exe /K \"cd.. && cd Mario Side-Scroller(Game) && java -jar mario.jar\"");

Ok so this did it

-1

You can try this:

Process p = Runtime.getRuntime().exec("java -jar \"your path to jar file\"");

Note must have \" chracter

Numb
  • 155
  • 3