org.paneris.util
Class SystemUtils

java.lang.Object
  extended by org.paneris.util.SystemUtils

public class SystemUtils
extends java.lang.Object

This class contains a number of static functions to run commands in new processes


Constructor Summary
SystemUtils()
           
 
Method Summary
static java.lang.String exec(java.lang.String cmd)
          Execute a system command and return its ouput in a String.
static java.lang.String execAsScript(java.lang.String script, java.lang.String scriptName)
          Write a command to a script and then execute that script.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SystemUtils

public SystemUtils()
Method Detail

exec

public static java.lang.String exec(java.lang.String cmd)
                             throws java.io.IOException
Execute a system command and return its ouput in a String.

It works by forking a separate process and capturing it's output stream (which is a java.lang.io.InputStream!)

Note that at present we ignore any InterruptedException which might get thrown whilst we are waiting for the process to terminate

Parameters:
cmd - System command to execute
Returns:
the command's output
Throws:
java.io.IOException - if there is a problem reading from the process' output stream
See Also:
InputStream, Process

execAsScript

public static java.lang.String execAsScript(java.lang.String script,
                                            java.lang.String scriptName)
                                     throws java.io.IOException
Write a command to a script and then execute that script. This can be used, for instance, to change the working directory before calling a command by using something like cmd = "#/bin/sh \n\n cd /some/where; ls"

If the script is not executable then this will fail silently. IT IS UP TO YOU TO MAKE THE FILE EXECUTABLE ON YOUR SYSTEM. YOU MUST ALSO MAKE THE CMD AN APPROPRIATE SCRIPT

TODO - can we chmod in Java?

Parameters:
script - text of a script to execute
scriptName - the filename to write the script to. The file should be executable (you might want to create it before hand and change permissions outside of java)
Returns:
output of the script
Throws:
java.io.IOException - if there is a problem reading from the script's output stream


Copyright © 2000-2008 PanEris. All Rights Reserved.