title: How to execute system commands (linux/bsd) using Java
body: <p>I am attempting to be cheap and execute a local system command (<code>uname -a</code>) in Java. I am looking to grab the output from <code>uname</code> and store it in a String. What is the best way of doing this? Current code:</p>
  
  <pre><code>public class lame {
  
      public static void main(String args[]) {
          try {
              Process p = Runtime.getRuntime().exec("uname -a");
              p.waitFor();
              BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
              String line=reader.readLine();
  
              while (line != null) {    
                  System.out.println(line);
                  line = reader.readLine();
              }
  
          }
          catch(IOException e1) {}
          catch(InterruptedException e2) {}
  
          System.out.println("finished.");
      }
  }
  </code></pre>