The code is fairly simple , which show currently running processes
Process p = Runtime.getRuntime().exec("TASKLIST");
BufferedReader reader = new BufferedReader(new InputStreamReader(
p.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
output on console i m getting in proper format
Image Name PID Session Name Session# Mem Usage
========================== ===================== ============
System Idle Process 0 Services 0 24 K
System 4 Services 0 1,260 K
svchost.exe 896 Services 0 7,060 K
taskmgr.exe 868 Console 2 13,300 K
WINWORD.EXE 5412 Console 2 39,860 K
iexplore.exe 5256 Console 2 69,104 K
eclipse.exe 1112 Console 2 1,788 K
javaw.exe 4380 Console 2 555,552 K
cmd.exe 1500 Console 2 3,264 K
conhost.exe 3120 Console 2 7,188 K
bash.exe 3360 Console 2 7,840 K
to show the output of the code on Jtextarea instead of
System.out.println(line);
i did
textarea.append(line+"\n");
but the formatting shows on text area is not proper as on console ,
Image Name PID Session Name Session# Mem Usage
========================= ======== ================ =========== ============
System Idle Process 0 Services 0 4 K
System 4 Services 0 10,024 K
smss.exe 324 Services 0 312 K
csrss.exe 436 Services 02,252 K
----------------------------------UPDATE-------------------------------
Formatting issue resolved by changing the font of Textarea , now its properly aligned
Seems that unlike the console, the font used by textarea
is not Monospaced Font.
Either set the JTextArea
's font to a monospaced font (or the logical Font.MONOSPACED
), or use a JTable
to display your data.
Check out this answer too