Home > Java > Java Code to View System Task List

Java Code to View System Task List

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.

Advertisements
  1. Srini
    February 23, 2010 at 11:59 am

    Nice one. It helped me to recollect the old class, RunTime. All the best in your blogging.

    • naveen
      November 23, 2011 at 11:56 am

      I get NoclassDefFoundError In running of program please suggest any idea to me urgent thanks ……

  2. Lorri
    February 24, 2010 at 11:28 am

    runTime.exec(“tasklist > ProcessList.txt”) would do the same 😉

    • February 24, 2010 at 1:56 pm

      Hi spam,
      It is good idea.I used this before sometime.You make remember this concept to me.

  3. March 27, 2010 at 11:46 pm

    D9uj3i vcsuglwnzkog, [url=http://odxuiyzmjemd.com/]odxuiyzmjemd[/url], [link=http://fmbgbjhxlicg.com/]fmbgbjhxlicg[/link], http://eimlokrlzblw.com/

  4. March 29, 2010 at 5:39 am

    hi
    i m using the code for access task manager but it some exception so plz give me some sugestion to correct them.

    • March 29, 2010 at 12:31 pm

      Hi Bhagewati,
      Send your exception detail.I will revert you shortly.

  5. April 3, 2010 at 4:26 pm

    wJv8FF amabazfrpzub, [url=http://pagpypmlvmmm.com/]pagpypmlvmmm[/url], [link=http://iyhmicgesklq.com/]iyhmicgesklq[/link], http://kxdhkinlclte.com/

  6. Heena
    May 22, 2010 at 9:05 am

    hey,this code is not giving the mentioned output to me,,,I dont know y,,,,,if i use dir insted of tasklist,then i got the directory record,but I m not getting the Processlist

    • RINKOO KUSHWAH
      October 20, 2012 at 7:14 pm

      You will get that process list in the file located in the directory where ur java program exists.
      Rinkoo Kushwah-7204619497

  7. August 4, 2010 at 9:40 am

    hi i want to get list of running websites in my computer… can anybody help me plzz..

  8. Arpit
    March 28, 2011 at 5:58 am

    i want to list currently running APPLICATIONS in Java. Like task manager shows. I am on windows machine. For clarification.. I don’t want to list processes, i want to list out the RUNNING USER APPLICATIONS.
    plz help me out…

  9. sam
    April 3, 2012 at 6:02 pm

    can i see the path also…using java code

  10. Parasu
    August 17, 2012 at 10:59 am

    Hi can you pls help me to get the list of Applications running from Task manager

  11. RINKOO KUSHWAH
    October 20, 2012 at 7:14 pm

    Its really a good program for that I was looking from long time………

  12. jahanzaib
    November 10, 2012 at 8:40 pm

    its really goof program and very help full thanx for it
    put i am not getting the aligned output when i run this on linux!!
    process with the path name effects the alignment! process that large path in length jumps into next line !! can you help me to align them or we can cmpress the process name ?

  13. April 20, 2013 at 6:59 am

    Hi… If you want more information on java and dissertation support, you may visit UK-Dissertation-Help.com and find java articles under archives or blog.

  14. suraj
    October 8, 2013 at 5:48 pm

    see after pressing the process button i m unable to get the list of processes. And why do we need to use process.txt file?

  15. Ranjit
    February 5, 2015 at 6:37 am

    thank to information sir but i need only application name current open or running on system for example open notepad ,paint,crome browser only name of application
    please reply sindhavranjit0101@gmail.com

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: