How to lock, shutdown, restart and suspend the pc with runtime.exec in java?

386 views Asked by At

How to lock, shutdown, restart and suspend the pc with runtime.exec in java?

I want to make a jar for windows and linux os.

I want to lock, shutdown, restart and suspend the pc programmatically.

I tried this code.

package poweroff;

import mousekeyboardcontrol.MouseKeyboardControl;


public class PowerOff {
String os;
Runtime runtime;
public PowerOff() {
    os = System.getProperty("os.name");
    runtime = Runtime.getRuntime();
}

public void shutdown() {     
    try {
        if ("Windows 8.1".equals(os) || "Windows 8.0".equals(os) || "Windows 10".equals(os)) {
            runtime.exec("shutdown -s");
        } else {
            System.out.println("Unsupported operating system");
        }
    } catch(Exception e) {
        System.out.println("shutdown error");
        e.printStackTrace();
    }
    
}

public void restart() {     
    try {
        if ("Windows 8.1".equals(os) || "Windows 8.0".equals(os) || "Windows 10".equals(os)) {
            runtime.exec("shutdown -r");
        } else {
            System.out.println("Unsupported operating system");
        }
    } catch(Exception e) {
        System.out.println("restart error");
        e.printStackTrace();
    }
    
}

public void suspend() {     
    try {
        if ("Windows 8.1".equals(os) || "Windows 8.0".equals(os) || "Windows 10".equals(os)) {
            runtime.exec("Rundll32.exe powrprof.dll,SetSuspendState Sleep");
        } else {
            System.out.println("Unsupported operating system");
        }
    } catch(Exception e) {
        System.out.println("suspend error");
        e.printStackTrace();
    }   
}

public void lock() {     
    try {
        if ("Linux".equals(os) || "Mac OS X".equals(os)) {
            new MouseKeyboardControl().ctrlAltL();
        } else if ("Windows 8.1".equals(os) || "Windows 8.0".equals(os) || "Windows 10".equals(os)) {
            runtime.exec("Rundll32.exe user32.dll,LockWorkStation");
        } else {
            System.out.println("Unsupported operating system");
        }
    } catch(Exception e) {
        System.out.println("pc lock error");
        e.printStackTrace();
    }
    
}

public static void main(String args[]) {
    PowerOff powerOff = new PowerOff();
    powerOff.lock();
}
}

But, it not worked.

I want to lock, shutdown, restart and suspend with programmatically in java for windows 7, 8, 10 and all linux os.

How can I solve this?

1

There are 1 answers

0
Spyros K On

A solution could be to use the command line tools of each OS with appropriate flags. See here for instance for linux. See also this answer on another example Shutting down a computer

For Windows shutdown try: "shutdown -s -t 0" For Windows Restart try: "shutdown -r -t 0" For Windows Hibernate try: "shutdown -h -t 0"

For Linux shutdown try: "shutdown now" For Linux restart try: "reboot now"

Tip: If you can introduce dependencies, using SystemUtils from Apache Commons could help check the OS version correctly in an easy to read/maintain manner.