title: Process Management using JAVA
body: <p>I am developing a swing application in java that shows the memory usage of each process on my pc</p>
  
  <p>The result should appear in a <code>JTable</code></p>
  
  <p>The <code>JTable</code> contains 4 columns :</p>
  
  <ul>
  <li>1) Process name   </li>
  <li> 2) PID </li>
  <li> 3) Session name  </li>
  <li> 4) Memory usage  </li>
  </ul>
  
  <p>The <code>JTable</code> appear like</p>
  
  <p><img src="https://i.stack.imgur.com/avSL9.png" alt="enter image description here"></p>
  
  <p>The <code>JTable</code> refresh itself each period using <code>ProcessBuilder</code> class </p>
  
  <p>The problem that i felt that is method is a little bit slow and boring when the user see it</p>
  
  <p>And my question:</p>
  
  <p>Is there any other powerful method ? or what can i do in my code to enhance this application?</p>
  
  <p>In the code :
  
  <li>
  The "processTable" field is the  <code>JTable</code> dragged from palette (of type <code>javax.swing.JTable</code>)
  <li>
  The "status" field is the <code>JLabel</code> dragged also from palette (of type <code>javax.swing.JLabel</code>)</p>
  
  <p>The method that i am used to display all the process is :</p>
  
  <pre><code>@SuppressWarnings("empty-statement")
      private void getProcessList() throws InterruptedException { 
          private DefaultTableModel table;        
          String filename = System.getProperty("java.io.tmpdir") + "\\list.txt", h;//to get %tmp%
          Scanner sc1 = null;
          int i, j, k = 0;
          table = (javax.swing.table.DefaultTableModel) processTable.getModel();
          processTable.editingStopped(null);
          table.setRowCount(0);
          try {
              ProcessBuilder builder = new ProcessBuilder("cmd.exe", "/c", "tasklist &gt; %tmp%\\list.txt");
              builder.redirectErrorStream(true);
              Process p = builder.start();
              p.waitFor();
              try {
                  sc1 = new Scanner(new File(filename));
                  while (sc1.hasNext()) {
                      h = sc1.nextLine();
                      k++;
                  }
                  k = k - 5;
              } catch (FileNotFoundException ex) {
                  System.out.println("File " + filename + " Not Found");
              };
              status.setText(" Processes: " + k);
              Object o[] = new Object[4];
              i = 0;
              try {
                  j = 0;
                  sc1 = new Scanner(new File(filename));
                  while (sc1.hasNext()) {
                      st = new StringTokenizer(sc1.nextLine(), "\n");
                      i++;
                      if (i &gt; 5) {
                          while (st.hasMoreTokens()) {
                              st1 = new StringTokenizer(st.nextToken(), " ");
                              if (st1.hasMoreTokens()) {
                                  o[0] = st1.nextToken();
                              }
                              if (st1.hasMoreTokens()) {
                                  h = st1.nextToken();
                                  if (h.contains(".exe")) {
                                      o[0] = o[0] + " " + h;
                                      o[1] = st1.nextToken();
                                  } else {
                                      o[1] = h;
                                  }
                              }
                              if (st1.hasMoreTokens()) {
                                  o[2] = st1.nextToken();
                              }
                              if (st1.hasMoreTokens()) {
                                  h = st1.nextToken();
                              }
                              if (st1.hasMoreTokens()) {
                                  o[3] = st1.nextToken();
                              }
                              if (st1.hasMoreTokens()) {
                                  h = st1.nextToken();
                              }
                              table.addRow(o);
                              j++;
                          }
                      } else {
                          if (st.hasMoreTokens()) {
                              h = st.nextToken();
                          }
                      }
                  }
              } catch (FileNotFoundException ex) {
                  System.out.println("File " + filename + " Not Found");
              } catch (Exception ex) {
                  System.out.println("UnknownError: " + ex.getMessage());
              } finally {
                  if (sc1 != null) {
                      sc1.close();
                  }
              }
          } catch (IOException ex) {
              Logger.getLogger(ProcessMan.class.getName()).log(Level.SEVERE, null, ex);
          }
  
      }
  </code></pre>