-2

I try

ProcessBuilder().command("C:\\Windows\\System32\\sc.exe query power");

or

ProcessBuilder().command("c:/windows/system32/sc.exe query power");

or

ProcessBuilder().command("c:/windows/system32/sc query power");

I always get the same error ...

Minstrel
  • 369
  • 1
  • 2
  • 14

1 Answers1

1

You should submit each argument for sc.exe separately to ProcessBuilder to avoid issues with argument escaping or quoting. Right now you have the whole command as a single String expression, this cause the problem.

Since C:\Windows\System32 directory should be in the system PATH it should enough to do.

ProcessBuilder pb = new ProcessBuilder("sc.exe", "query", "power");
Process p = pb.start();
int result = p.waitFor();
Karol Dowbecki
  • 43,645
  • 9
  • 78
  • 111
  • ultimately, I used `new ProcessBuilder("sc", "query", "power");` so yes it's in the path and yes I think it's the best way to do, because "cmd arg1 arg2 etc.." is interpreted as a single exe name (so with spaces) by the jvm thanks ! – Minstrel Apr 11 '19 at 16:18