Closed ghost closed 4 years ago
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?
You can check Operating System first and based on that you can issue command. I no longer support this project but StackOverflow will definitely help you :)
How can you do this code?
Can you give any information ?
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.
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?