2009-10-20 32 views

回答

3

來自:a 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(); 
    } 
} 

} 
+2

但這是OS依賴! –

+0

是的,只需讀取代碼,就可以更新runtime.exec中的內容,以便它可以是Linux命令或任何其他操作系統命令。Linux「shutdown -h now」或「init 0」.. –

1

你可以在XP上執行shutdown DOS命令和更高

2

不能直接關閉OS和Java,但你可以執行shell腳本或本地程序e.g Runtime.getRuntime().exec("shutdown")。或者你可以寫入JNI掛鉤到本機系統

5

我相信由於Java是獨立於操作系統的,所以沒有直接的方法來實現它,除了調用底層操作系統的東西。

+2

Java可以提供一種與操作系統無關的關閉機器的方法。 –

+0

但它沒有。這是好的:) –

1
// 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) {} 

:-)