import java.io.*; import java.util.*; class ParseAndStoreCommandOutput { public static void main(String args[]) { // Each array is used to store an individual "column" of the output.... ArrayList< String > ProcessNames = new ArrayList< String >(); ArrayList< String > ProcessIDs = new ArrayList< String >(); ArrayList< String > OtherInfo = new ArrayList< String >(); try { // go execute the command as an externeal process Process p = Runtime.getRuntime().exec("ps"); // NOTE: Ethan, you'll want to change the above command "ps -ax" to // "tasklist" for this to work on Windows. // "connect" to the output of that commnad. This gives us a new object // called "pOutput" which, as we'll see, is the output of the above // process. We can read from it just as we would read from a file. BufferedReader pOutput = new BufferedReader(new InputStreamReader(p.getInputStream())); // read the output of the process one line at a time... String line; while ((line = pOutput.readLine()) != null) { // now, we parse this line into "tokens" (breaking it up by spaces) by doing // the following... StringTokenizer tokenizer = new StringTokenizer(line); // Just grab the first 3 tokens and put them into the arrays... ProcessNames.add( tokenizer.nextToken() ); ProcessIDs.add( tokenizer.nextToken() ); OtherInfo.add( tokenizer.nextToken() ); } } catch (Exception e) { } // Now, we output whatever was stored in the arrays... System.out.print("PROCESS NAME PROCESS ID OTHER INFO\n"); System.out.print("------------ ---------- ----------\n"); for (int i = 0; i < ProcessNames.size(); i++) { System.out.print( ProcessNames.get(i) ); System.out.print("\t"); System.out.print( ProcessIDs.get(i) ); System.out.print("\t"); System.out.print( OtherInfo.get(i) ); System.out.print("\n"); } } }