3

So i have this code which prints out the running processes from task manager to the ide in netbeans but i'd like to scan the output of processes so i can list the highest running process on my PC at the time?

public static void main(String[] args) throws IOException {
  try {
   String tasklist;
   Process p = Runtime.getRuntime().exec
    (System.getenv("windir") +"\\system32\\"+"tasklist.exe");
    try (BufferedReader input = new BufferedReader(new   InputStreamReader(p.getInputStream()))) {
      while ((tasklist = input.readLine()) != null) {
      System.out.println(tasklist); //<-- Parse data here.
  }
}
} catch (IOException err) {
  err.printStackTrace();
} 
}

This give me the below output for example and if i'd like to kill netbeans because it's running over 500,000k how would i do that?

Image Name                     PID Session Name        Session    Mem Usage

chrome.exe                    3464 Console                    3    159,672 K
netbeans64.exe                3664 Console                    3    592,216 K
chrome.exe                    3808 Console                    3    384,556 K
java.exe                      2040 Console                    3     36,732 K
tasklist.exe                  2208 Console                    3      6,196 K
GhostCat
  • 127,190
  • 21
  • 146
  • 218
Codeo
  • 33
  • 5
  • Well, if you are running this *inside* netbeans, you had better not kill it or you won't be able to see the list anymore... In any case, in your program, you have the `tasklist` line. Parse it into meaningful data, put it in a list of objects, sort it, and do whatever you need with the sorted list. – RealSkeptic Mar 01 '17 at 13:48
  • 3
    And hint: feel free to give feedback to my answer via comments. I have seen it too often the last days that some newbie asks a questions ... and doesn't ever come back. No comment, no upvote, no accept. So, please consider providing some form of feedback ;-) – GhostCat Mar 01 '17 at 13:59
  • Example, now that you reach upvote rep, you could upvote ... and leave a comment ;-) – GhostCat Mar 01 '17 at 14:07

1 Answers1

4

You iterate that data; and collect those attributes that you are interested in; for example name, PID, and of course, mem usage.

You would probably create a class like

public class RunningProcessInfo {
  private final String name ...

  public RunningProcessInfo(String name, int pid, int memUsage) {

In other words: you write parsing code that takes a line as

chrome.exe 3464 Console 3 159,672 K

and turns that into

new RunningProcessInfo("chrome.exe", 3464, 159672)

(by parsing the string input; and turning strings like "3464" into numbers, and so on).

Meaning: you replace the line System.out.println(tasklist); with something along the lines of

processInfos.add( parseLine(tasklist) );

with processInfos being some ArrayList<RunningProcessInfo> and parseLine() being that method that takes a single line and fetches the data to build a new RunningProcessInfo object.

You collect those objects in a list. Then you iterate that list to find all objects that meet a certain criteria.

Finally you use another process builder to send a command to kill those PIDs you intend to kill ( see here for examples how to do that ). And of course, you pay attention - as you for sure do not want to kill your netbeans session while that session is running your program ...

Community
  • 1
  • 1
GhostCat
  • 127,190
  • 21
  • 146
  • 218
  • Thanks for the help and quick response! Have you got any recommendations for iterating my data to collect the attributes? – Codeo Mar 01 '17 at 14:19
  • I enhanced the answer about the "looping" part (as that is already there). The the parsing, you can get ideas here http://stackoverflow.com/questions/11871520/how-can-i-read-input-from-the-console-using-the-scanner-class-in-java (you can use a scanner; just not in System.in; but your file) or you go with a tokenizer: http://stackoverflow.com/questions/691184/scanner-vs-stringtokenizer-vs-string-split ... many options; up to you. So, please step back, and digest this first; and when you have a really specific question ... write up a new one. – GhostCat Mar 01 '17 at 14:28