Author Archive
Java Code to View System Task List
February 20, 2010
19 comments
Introduction
In our day-to-day life, lots of process are running in our system. For windows machine we use Task Manager and for linux machine we use terminal to view the current tasks running in the system. In this article let us see the tasks which are running in our system using a java code. Our sample code will store the current tasks in a file and store it in a desired location.
Code
/* * This code get the process list which are currently running(shows in your task manager) in your system and * stored at the file named as "process.txt" * * Remarks: * The file is stored at where this java file is available. * Chache the file path which you need */ package com.sample.javaapplication; import java.io.*; import java.util.StringTokenizer; /** * * @author Muneeswaran */ public class GetProcessList { private String GetProcessListData() { Process p; Runtime runTime; String process = null; try { System.out.println("Processes Reading is started..."); //Get Runtime environment of System runTime = Runtime.getRuntime(); //Execute command thru Runtime p = runTime.exec("tasklist"); // For Windows //p=r.exec("ps ux"); //For Linux //Create Inputstream for Read Processes InputStream inputStream = p.getInputStream(); InputStreamReader inputStreamReader = new InputStreamReader(inputStream); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); //Read the processes from sysrtem and add & as delimeter for tokenize the output String line = bufferedReader.readLine(); process = "&"; while (line != null) { line = bufferedReader.readLine(); process += line + "&"; } //Close the Streams bufferedReader.close(); inputStreamReader.close(); inputStream.close(); System.out.println("Processes are read."); } catch (IOException e) { System.out.println("Exception arise during the read Processes"); e.printStackTrace(); } return process; } private void showProcessData() { try { //Call the method For Read the process String proc = GetProcessListData(); //Create Streams for write processes //Given the filepath which you need.Its store the file at where your java file. OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream("ProcessList.txt")); BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter); //Tokenize the output for write the processes StringTokenizer st = new StringTokenizer(proc, "&"); while (st.hasMoreTokens()) { bufferedWriter.write(st.nextToken()); //Write the data in file bufferedWriter.newLine(); //Allocate new line for next line } //Close the outputStreams bufferedWriter.close(); outputStreamWriter.close(); } catch (IOException ioe) { ioe.printStackTrace(); } } public static void main(String[] args) { GetProcessList gpl = new GetProcessList(); gpl.showProcessData(); } }
Now let us execute our sample program, the output would be the following
Image Name PID Session Name Session# Mem Usage ========================= ======== ================ =========== ============ System Idle Process 0 Services 0 24 K System 4 Services 0 3,056 K smss.exe 412 Services 0 732 K csrss.exe 548 Services 0 6,076 K wininit.exe 592 Services 0 3,940 K csrss.exe 604 Console 1 8,088 K services.exe 636 Services 0 6,668 K lsass.exe 648 Services 0 2,496 K lsm.exe 660 Services 0 4,016 K winlogon.exe 760 Console 1 5,612 K svchost.exe 836 Services 0 6,328 K svchost.exe 924 Services 0 7,240 K svchost.exe 964 Services 0 40,584 K svchost.exe 1032 Services 0 14,628 K svchost.exe 1084 Services 0 70,888 K svchost.exe 1100 Services 0 28,736 K audiodg.exe 1228 Services 0 14,368 K svchost.exe 1264 Services 0 4,760 K SLsvc.exe 1280 Services 0 10,936 K svchost.exe 1324 Services 0 13,552 K svchost.exe 1452 Services 0 16,036 K upeksvr.exe 1568 Console 1 9,496 K WLTRYSVC.EXE 1656 Services 0 2,700 K BCMWLTRY.EXE 1704 Services 0 20,668 K aswUpdSv.exe 1720 Services 0 388 K wlanext.exe 1736 Services 0 5,108 K ashServ.exe 1788 Services 0 22,492 K spoolsv.exe 1628 Services 0 9,484 K svchost.exe 1732 Services 0 17,292 K AERTSrv.exe 2008 Services 0 1,620 K httpd.exe 1888 Services 0 50,016 K AppleMobileDeviceService. 2056 Services 0 3,536 K mDNSResponder.exe 2068 Services 0 4,448 K mysqld-nt.exe 2140 Services 0 53,444 K o2flash.exe 2180 Services 0 2,480 K svchost.exe 2200 Services 0 5,192 K sprtsvc.exe 2372 Services 0 1,140 K svchost.exe 2432 Services 0 6,232 K svchost.exe 2460 Services 0 2,156 K SearchIndexer.exe 2480 Services 0 32,016 K ACFXAU32.exe 2508 Services 0 2,492 K httpd.exe 2876 Services 0 51,208 K taskeng.exe 3524 Services 0 5,736 K ashMaiSv.exe 3572 Services 0 1,632 K ashWebSv.exe 3596 Services 0 16,152 K taskeng.exe 2148 Console 1 10,556 K dwm.exe 804 Console 1 65,228 K explorer.exe 3936 Console 1 65,360 K MSASCui.exe 2176 Console 1 8,884 K Apoint.exe 3996 Console 1 5,784 K OEM13Mon.exe 3640 Console 1 4,444 K hkcmd.exe 1992 Console 1 4,908 K igfxpers.exe 4100 Console 1 4,024 K DellWMgr.exe 4116 Console 1 10,812 K igfxsrvc.exe 4124 Console 1 5,560 K WLTRAY.EXE 4148 Console 1 20,620 K PDVDDXSrv.exe 4180 Console 1 7,984 K sprtcmd.exe 4188 Console 1 1,692 K ashDisp.exe 4196 Console 1 1,784 K jusched.exe 4212 Console 1 8,408 K sidebar.exe 4232 Console 1 28,612 K googletalk.exe 4256 Console 1 29,780 K YahooMessenger.exe 4272 Console 1 51,152 K wmpnscfg.exe 4288 Console 1 5,100 K SSScheduler.exe 4336 Console 1 3,084 K quickset.exe 4344 Console 1 10,548 K wweb32.exe 4352 Console 1 8,036 K psqltray.exe 4560 Console 1 16,380 K unsecapp.exe 4676 Console 1 4,768 K ApMsgFwd.exe 4748 Console 1 2,816 K WmiPrvSE.exe 4804 Services 0 6,824 K wmpnetwk.exe 4904 Services 0 11,144 K ApntEx.exe 4976 Console 1 3,744 K hidfind.exe 5024 Console 1 3,404 K WmiPrvSE.exe 5900 Services 0 9,488 K firefox.exe 5176 Console 1 197,884 K netbeans.exe 3896 Console 1 223,788 K googletalkplugin.exe 4740 Console 1 10,216 K WinRAR.exe 5576 Console 1 15,628 K SearchProtocolHost.exe 4636 Services 0 8,364 K SearchFilterHost.exe 3952 Services 0 6,636 K java.exe 5360 Console 1 10,308 K tasklist.exe 4832 Console 1 4,644 K
I hope this article helps to view the task list of a Windows or a Linux machine using Java code. If you find this article is useful to you. Don’t forget to leave your valuable comments.
Recent Comments