View Javadoc

1   package org.paneris.util;
2   
3   import java.io.FileWriter;
4   import java.io.IOException;
5   import java.io.InputStream;
6   
7   /**
8    * This class contains a number of static functions to run
9    * commands in new processes
10   */
11  
12  public class SystemUtils {
13  
14      /**
15       * Execute a system command and return its ouput in a String.
16       * <P>
17       * It works by forking a separate process and capturing it's
18       * output stream (which is a java.lang.io.InputStream!)
19       * <P>
20       * Note that at present we ignore any InterruptedException
21       * which might get thrown whilst we are waiting for the process
22       * to terminate
23       *
24       * @param cmd System command to execute
25       * @return the command's output
26       * @exception IOException if there is a problem reading from
27       *            the process' output stream
28       * @see     java.io.InputStream
29       * @see     java.lang.Process
30       */
31      public static String exec(String cmd) throws IOException {
32  
33          // Run the command
34          Process proc  = Runtime.getRuntime().exec(cmd);
35  
36          // Copy output of cmd to a StringBuffer
37          StringBuffer output = new StringBuffer();
38          InputStream proc_output = proc.getInputStream();
39          int b=0;
40          while( (b=proc_output.read()) >= 0 ) {
41               output.append((char)b);
42          }
43  
44          // Clean up the process
45          try {
46              proc.waitFor();
47          } catch (InterruptedException interupt) {
48              // Another thread has interrupted our waiting
49              // Should we do anything?
50          }
51          proc.destroy();
52  
53          return new String(output);
54      }
55  
56  
57      /**
58       * Write a command to a script and then execute that script. This can be
59       * used, for instance, to change the working directory before calling
60       * a command by using something like cmd = "#/bin/sh \n\n cd /some/where; ls"
61       * <P>
62       * If the script is not executable then this will fail silently. IT IS
63       * UP TO YOU TO MAKE THE FILE EXECUTABLE ON YOUR SYSTEM. YOU MUST ALSO MAKE
64       * THE CMD AN APPROPRIATE SCRIPT
65       * <P>
66       * TODO - can we chmod in Java?
67       * <P>
68       * @param script text of a script to execute
69       * @param scriptName the filename to write the script to. The file should
70       *                   be executable (you might want to create it before hand
71       *                   and change permissions outside of java)
72       * @return output of the script
73       * @exception IOException if there is a problem reading from
74       *            the script's output stream
75       *
76       */
77      public static String execAsScript(String script, String scriptName) throws IOException {
78          FileWriter scriptWriter = new FileWriter(scriptName);
79          scriptWriter.write(script,0,script.length());
80          scriptWriter.close();
81          return exec(scriptName);
82      }
83  
84  }