import java.util.*;
import java.io.*;
class StreamGobbler extends Thread
{
InputStream is;
String type;
StringBuffer sbOutput = new StringBuffer("");
StreamGobbler(InputStream is, String type)
{
this.is = is;
this.type = type;
}
public void run()
{
try
{
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line=null;
while ( (line = br.readLine()) != null) {
sbOutput.append(line);
sbOutput.append("\n");
}
} catch (IOException ioe)
{
ioe.printStackTrace();
}
}
public String getOutput()
{
return sbOutput.toString();
}
}
public class LocalExec
{
private boolean returnBool = false;
private String errorMsg = "";
private String outputMsg = "";
public String getOutput() {
return outputMsg;
}
public String getError() {
return errorMsg;
}
public boolean cmdExec(String arg)
{
int returnInt=-1;
try
{
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(arg);
StreamGobbler errorGobbler = new
StreamGobbler(proc.getErrorStream(), "ERROR");
StreamGobbler outputGobbler = new
StreamGobbler(proc.getInputStream(), "OUTPUT");
errorGobbler.start();
outputGobbler.start();
// Wait for process to complete
returnInt = proc.waitFor();
// Load Message variables
errorMsg = errorGobbler.getOutput();
outputMsg = outputGobbler.getOutput();
if (returnInt == 0 ) {
returnBool = true;
}
} catch (Throwable t)
{
t.printStackTrace();
}
return returnBool;
}// End of cmdExec(String)
public boolean cmdExec(String arg,String workingDirectory)
{
int returnInt=-1;
try
{
File lclFile=null;
if(workingDirectory!=null)
lclFile=new File(workingDirectory);
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(arg,null,lclFile);
StreamGobbler errorGobbler = new
StreamGobbler(proc.getErrorStream(), "ERROR");
StreamGobbler outputGobbler = new
StreamGobbler(proc.getInputStream(), "OUTPUT");
errorGobbler.start();
outputGobbler.start();
// Wait for process to complete
returnInt = proc.waitFor();
// Load Message variables
errorMsg = errorGobbler.getOutput();
outputMsg = outputGobbler.getOutput();
if (returnInt == 0 ) {
returnBool = true;
}
} catch (Throwable t)
{
t.printStackTrace();
}
return returnBool;
}// End of cmdExec(String,String)
}// End of LocalExec