import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class ProcessTest {
public static void main(String[] args) {
ProcessBuilder pb = new ProcessBuilder ( "tasklist");
try {
Process process = pb.start();
InputStream fis = process.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
String line = null;
StringBuffer cmdout = new StringBuffer();
while ((line = br.readLine()) != null) {
cmdout.append(line).append("\n");
}
System.out.println(cmdout.toString().trim());
} catch (IOException e) {
e.printStackTrace();
}
}
}
输出如下:
图像名 PID 会话名 会话# 内存使用
========================= ====== ================ ======== ============
System Idle Process 0 Console 0 28 K
System 4 Console 0 324 K
smss.exe 1076 Console 0 812 K
csrss.exe 1152 Console 0 3,296 K
winlogon.exe 1176 Console 0 31,580 K
services.exe 1220 Console 0 4,684 K
lsass.exe 1232 Console 0 1,672 K
svchost.exe 1408 Console 0 6,236 K
svchost.exe 1496 Console 0 5,036 K
svchost.exe 1656 Console 0 38,656 K
spoolsv.exe 1872 Console 0 8,000 K
explorer.exe 332 Console 0 18,888 K
avp.exe 376 Console 0 24,960 K
db2dasrrm.exe 460 Console 0 34,652 K
TSVNCache.exe 672 Console 0 12,476 K
igfxtray.exe 1380 Console 0 7,344 K
hkcmd.exe 1236 Console 0 4,056 K
igfxpers.exe 1428 Console 0 3,468 K
db2mgmtsvc.exe 1444 Console 0 10,072 K
RTHDCPL.exe 1460 Console 0 32,480 K
igfxsrvc.exe 1572 Console 0 3,772 K
avp.exe 1680 Console 0 6,008 K
db2systray.exe 1700 Console 0 32,512 K
ctfmon.exe 1780 Console 0 4,892 K
picpick.exe 1984 Console 0 3,600 K
QQ.exe 2024 Console 0 34,648 K
dsNcService.exe 264 Console 0 3,880 K
365日历.EXE 1952 Console 0 43,788 K
CLCL.exe 1028 Console 0 8,252 K
klnagent.exe 1052 Console 0 3,196 K
thunderbird.exe 352 Console 0 38,692 K
rtxc.exe 472 Console 0 29,968 K
db2rcmd.exe 1836 Console 0 11,832 K
TXPlatform.exe 2488 Console 0 3,808 K
firefox.exe 2724 Console 0 195,912 K
cmd.exe 2716 Console 0 52 K
sh.exe 3936 Console 0 152 K
conime.exe 2752 Console 0 3,424 K
eclipse.exe 3060 Console 0 2,592 K
JAVAW.EXE 2984 Console 0 446,692 K
EXCEL.EXE 3232 Console 0 1,936 K
wmiprvse.exe 4084 Console 0 6,368 K
JAVAW.EXE 320 Console 0 6,860 K
tasklist.exe 2936 Console 0 4,812 K
当运行其他命令 ,如dir等时,用如下写法:
ProcessBuilder pb = new ProcessBuilder ( "cmd", "/c", "dir");