0

Is there any method of running a jar file from java using something like process builder? I've tried this method but having no luck as I keep getting CreateProcess error=2, The system cannot find the file specified. I've ran the same script outside of the java program into a powershell window and it works just fine.

Code I've tried

ProcessBuilder processBuilder = new ProcessBuilder("Powershell.exe java -jar D:\\Outlook.jar");
Process process = processBuilder.start();
process.waitFor();
Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
  • Try splitting the command line arguments up in separate strings (see [documentation of `ProcessBuilder`](https://docs.oracle.com/javase/7/docs/api/java/lang/ProcessBuilder.html)) – Quaffel Nov 18 '19 at 11:58
  • Does `new ProcessBuilder("Powershell.exe", "java", "-jar", "D:\\Outlook.jar");` work? You may have to specify the full path of the executable, even if it is the Powershell, but try without first... – deHaar Nov 18 '19 at 12:08
  • I tried a sort of different way using the paths to java and the jar and using runtime.getRuntime().exec(command) instead of processbuilder with the command being "path to java" -Xms400M -Xmx400M -Djava.library.path=lib -client -jar "path to jar" –  Nov 18 '19 at 12:25

1 Answers1

1

This code works

String command = "\"path to java exe\" -Xms400M -Xmx400M -Djava.library.path=lib -client -jar \"path to jar\"";
System.out.println(command);
Process proc = Runtime.getRuntime().exec(command);
proc.waitFor();