title: Getting A List of System Processes in Java Using Hyperic SIGAR
body: <p>I am trying to figure out how to get a <code>list</code> of system processes using <code>Hyperic SIGAR</code>. This is what I have so far: </p>
  
  <pre><code>import org.hyperic.sigar.Humidor;
  import org.hyperic.sigar.ProcExe;
  import org.hyperic.sigar.SigarException;
  import org.hyperic.sigar.SigarProxy;
  
  public class ProcessReader 
  {
      Humidor h; 
      public ProcessReader()
      {
          h = Humidor.getInstance(); 
      }
      public void printProcessList()
      {
          SigarProxy sp = h.getSigar();
          try 
          {
              long[] pidList = sp.getProcList();
              for(int i=0; i&lt;pidList.length; i++)
              {
                  ProcExe temp = sp.getProcExe(pidList[i]); 
                  String exeName = temp.getName(); 
                  System.out.println(pidList[i] + " " + exeName); 
              }
          } catch (SigarException e) {
              e.printStackTrace();
          } 
      }
  }
  </code></pre>
  
  <p>However, this just ends up throwing a SIGAR exception after a seemingly-arbitrary number of iterations. For example, once, I got this: </p>
  
  <pre><code>1313 /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/bin/java
  
  1311 /System/Library/Frameworks/CoreServices.framework/Frameworks/Metadata.framework/Versions/A/Support/mdworker
  </code></pre>
  
  <p>and then it threw a <code>Sigar Exception</code> for an invalid argument at the line: </p>
  
  <p><code>ProcExe temp = sp.getProcExe(pidList[i]);</code></p>
  
  <p>Sometimes, it runs for more than two iterations. Sometimes, it runs for less than two iterations. I'm not quite sure what's going on, and the <code>SIGAR</code> documentation doesn't seem to be very helpful. When I wanted to access the CPU and RAM usage information, I used a similar method of getting a <code>Humidor</code>, getting a <code>SIGARProxy</code> from it, and then getting the information objects (CPU and Mem, respectively) from the <code>SIGARProxy</code>. However, doing this for the system processes doesn't seem to be working very well. How am I supposed to do this?</p>