`
pqcc
  • 浏览: 125215 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Java获取系统所有进程

阅读更多
代码比较简单,就不那么讲究了。一个 main 写了吧。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import org.apache.log4j.Logger;
/**
* @author pqcc
*/

public class ProcesserTest
{
private static Logger log = Logger.getLogger(ProcesserTest.class.getName());
public static void main(String[] args)
{
Process process = null;
try {
/**
*  tasklist 或 ipconfig 只要在cmd 模式下可运行都可以.
*/
process = Runtime.getRuntime().exec("cmd.exe   /c   tasklist");
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();
}
}

}





-----------------------------
简单修改了下,没有 UNIX 的环境,所以也不知道是否能够正常运行。(类名有所变化)



/**
* @author pqcc
*/
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;
}

}
分享到:
评论
4 楼 shiren1118 2008-05-26  
Groovy in action 中 有相应例子,呵呵,更简洁
3 楼 pqcc 2008-05-25  
fxsjy 写道
晕,原来如此,最好再加上系统判断吧,跨平台:“ps aux”

简单修改了下,没有 UNIX 的环境,所以也不知道是否能够正常运行。(类名有所变化)



/**
* @author pqcc
*/
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;
}

}
2 楼 qingjian 2008-05-25  
fxsjy 写道
晕,原来如此,最好再加上系统判断吧,跨平台:“ps aux”

?? 为什么 难道 在java 上面不同的系统 去进程还有不同的 ? 学习中

看了一下代码明白了!
1 楼 fxsjy 2008-05-25  
晕,原来如此,最好再加上系统判断吧,跨平台:“ps aux”

相关推荐

Global site tag (gtag.js) - Google Analytics