Reputation: 17
I'm able to run same code through a normal java class. However when I move same code to a service class in springboot, and call the service from controller on a hit of url, the command doesn't run or gets stuck for a long time.
Process process = new ProcessBuilder("CMD", "/C", command2).start();
.
.
.
process.waitFor();
process.destroy();
Tried a lot of times and different ways, still not able to find the solution.
command is
"tool --f pathToFile".
Upvotes: 1
Views: 2022
Reputation: 271
Try to use separate arguments and dont use entire command but break it into separate argumennts as below:
try {
List<String> commands = new ArrayList<>();
commands.add("CMD");
commands.add("/C");
commands.add("tool");
commands.add("--f");
commands.add(pathToFile);
ProcessBuilder pb = new ProcessBuilder(commands);
try {
Process p = pb.start();
int j = p.waitFor();
int exitValue = p.exitValue();
System.out.println("Finished with code: " + j);
System.out.println("Finished with exitValue: " + exitValue);
} catch (Exception e) {
System.out.println("exception: " + e);
}
} catch (Exception e) {
e.printStackTrace();
}
Upvotes: 1