代码比较简单,就不那么讲究了。一个 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;
}
}
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;
}
}