How to close your OS using Java

How to close your OS using Java?

+4
source share
5 answers

from: blog

package com.deepak.entertainment; import java.io.IOException; import java.io.OutputStream; public class Deepak { public static void main(String[] args) throws InterruptedException { Runtime runtime = Runtime.getRuntime(); try { Process process = runtime.exec("C:\\WINDOWS\\system32\\cmd.exe"); OutputStream os = process.getOutputStream(); os.write("shutdown -s -f -t 90 \n\r".getBytes()); os.close(); process.waitFor(); } catch (IOException e) { e.printStackTrace(); } } } 
+3
source

I believe that since Java is OS independent, there is no direct way to do this other than calling something from the underlying OS.

+5
source

You cannot directly close the OS using Java, but you can run a shell script or your own program, such as Runtime.getRuntime().exec("shutdown") . Or you can write JNI hook in your own system

+2
source

You can execute the shutdown DOS command on XP and above

+1
source
 // May need to change for a Swing or SWT application. System.out.println ("Please shut down your OS now. I'll wait..."); boolean forever = true; while (forever) {} 

:-)

+1
source

All Articles