'Runtime.getRuntime().exec() - some commands working in IDE, but not from an executable .jar file?
I exported my Maven Spring Boot project as a .jar file, and everything seems to be working fine, except for a few commands that my app is supposed to run in the Terminal.
For example "shutdown now" will run, and turn off the PC, but "xdg-open somelink.com" doesn't do anything?
I've tried executing the terminal commands in the following two ways:
public void runInTerminal(String cmd){
ProcessBuilder processBuilder = new ProcessBuilder();
processBuilder.command("bash", "-c", cmd);
try {
Process process = processBuilder.start();
StringBuilder output = new StringBuilder();
BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
output.append(line + "\n");
}
int exitVal = process.waitFor();
if (exitVal == 0) {
System.out.println("Success!");
System.out.println(output);
System.exit(0);
} else {
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
And also tried
public void commandRun(String cmd) throws IOException, InterruptedException {
Runtime run = Runtime.getRuntime();
Process pr = run.exec(cmd);
pr.waitFor();
}
Thank you for your time!
Solution 1:[1]
Check that <jre-dir>/lib/jspawnhelper/jspawnhelper
have the -X flag
(executable) enabled.
ProcessBuilder
is a wrapper for Runtime.exec
who is exeucte commands nativly throu the OS. Without the executable-flag on os-level you can not execute commands.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Grim |