简体   繁体   中英

Java ProcessBuilder Shutdown System Error

 ProcessBuilder pb;
 Process process;
 String command ="shutdown -s";
    try {
        pb = new ProcessBuilder("cmd.exe", "/C", command) 
        process = pb.start();
        process.waitFor();
        if (process.exitValue() == 0) {                  
            //success
        } else {
            //handle error
        }
    } catch (Exception e) {
        //handle error
    }

When I try to get inputstream and run that block of code system goes into an infinite loop. Then I changed the code as seen above. However when I run it it gets exit value of 1 and can not shutdown system.

Any ideas?

PS: I don't want to use java run time.

Try:

pb = new ProcessBuilder("cmd.exe", "/C", "shutown", "-s");

the argument(s) command for the constructor ProcessBuilder(String... command) are passed each as 1 argument to the executable, this allows to have spaces in the argument.

the way you are executing the command is equivalent to

cmd /C "shutdown -s"

thus "shutdown -s" is interpreted as a single argument.

command should be:

String command ="shutdown.exe -s";

instead of:

String command ="shutdown -s";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM