2011-07-12 16 views

回答

6
try { 
    // Execute command 
    String command = "ps aux"; 
    Process child = Runtime.getRuntime().exec(command); 

    // Get the input stream and read from it 
    InputStream in = child.getInputStream(); 
    int c; 
    while ((c = in.read()) != -1) { 
     process((char)c); 
    } 
    in.close(); 
} catch (IOException e) { 
} 

源(修改):http://www.exampledepot.com/egs/java.lang/ReadFromCommand.html

+0

不錯!比我的回答更具描述性! – Davidann

+0

謝謝,但我或多或少地從提到的頁面拷貝了示例;) –

+0

謝謝,這正是我所尋找的。 – Mustafa

1

在使用/ proc虛擬文件系統的系統中,您可以橫切目錄並找出/ proc下的信息。

/proc中的編號目錄是正在運行的進程的進程ID,並且這些目錄中的項目有助於描述進程。

對於內存使用情況和cpu信息,有/ proc/meminfo和/ proc/cpuinfo(以及更多)。希望這會讓你開始。

對於沒有在/ proc虛擬文件系統,你需要使用JNI綁定到C代碼將做內核API調用,或嘗試運行本地命令行程序徹底而管道Exec,並解析輸出反饋到系統Java程序。

相關問題