Advertisement
radoslav1992

ProcessInformation

Apr 6th, 2020
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.69 KB | None | 0 0
  1. package com.bulpros.javaknights.social;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8. import java.util.regex.Matcher;
  9. import java.util.regex.Pattern;
  10.  
  11. public class ProcessInformation {
  12.     public static void main(String[] args) throws IOException {
  13.         List<ProcessInfo> processMyInfo = getProcesses(100000);
  14.     }
  15.  
  16.     public static List<ProcessInfo> getProcesses(int memory) throws IOException {
  17.         List<ProcessInfo> myResult = new ArrayList<>();
  18.         try {
  19.             String line;
  20.             Process prc = Runtime.getRuntime().exec(System.getenv("windir") +"\\system32\\"+"tasklist.exe");
  21.             BufferedReader input = new BufferedReader(new InputStreamReader(prc.getInputStream()));
  22.             int counter = 0;
  23.             while ((line = input.readLine()) != null) {
  24.                 counter++;
  25.                 if(counter > 3) {
  26.                     String processName = line.split("[ ]{2,}")[0];
  27.                     Pattern pattern = Pattern.compile("�| [K]");
  28.                     Matcher matcher = pattern.matcher((line.split("[ ]{2,}")[3]));
  29.                     int processMemory = Integer.parseInt(matcher.replaceAll(""));
  30.                     if(processMemory > memory) {
  31.                         ProcessInfo processInfo = new ProcessInfo(processName, processMemory);
  32.                         myResult.add(processInfo);
  33.                         processInfo.printAsString();
  34.                     }
  35.                 }
  36.             }
  37.             input.close();
  38.         } catch (IOException e) {
  39.             e.printStackTrace();
  40.         }
  41.         return myResult;
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement