`

Java获取系统所有进程

阅读更多

import java.io.BufferedReader; 

import java.io.IOException; 

import java.io.InputStreamReader; 

import org.apache.log4j.Logger; 

public class ProcessorTest 

private static Logger log = Logger.getLogger(ProcessorTest.class.getName()); 

public static void main(String[] args) { 

    new ProcessorTest().printSystemProcessor(); 

 

public void printSystemProcessor(){ 

Process process = null; 

String command = getExecCommand(); 

try { 

process = Runtime.getRuntime().exec(command); 

BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream())); 

String line = ""; 

while ((line = input.readLine()) != null){ 

log.info(line); 

input.close(); 

} catch (IOException e) { 

e.printStackTrace(); 

 

public String getExecCommand() { 

// default is windows. 

String command = "cmd.exe /c tasklist"; 

String osName = System.getProperty("os.name"); 

if(osName != null && osName.indexOf("Windows")<0){ 

command = "ps aux"; 

return command; 

}

//新线程

new Thread() {

public void run() {

try {

Process ps = Runtime.getRuntime().exec(batName);

ps.waitFor();

} catch (IOException e) {

e.printStackTrace();

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}.start();

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics