|
Contact me sending an e-mail (antispam defense activated) |
Title: Execute a command
Author: Sandro Tosi
Last modified: 2005-02-06
The following class let you execute a command.
It's even possible to execute via rsh, replacing the command name with
the complete command string to execute (that is, you can execute every
command, using this class...)
import java.io.*;
public class ExecCommand
{
public static void main(String args[]) throws Exception
{
try{
Runtime load = Runtime.getRuntime();
String command2execute = "<the command to be executed>";
Process process = Runtime.getRuntime().exec(command2execute);
BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
String line;
//Processes output (Right way to manage process.waitFor() method)
while ((line = reader.readLine()) != null) {
//ingore output; uncomment it to obtain it
//System.out.println(line);
}
reader.close();
// This wait until the end of the process launched
int retVal = process.waitFor();
}catch(Exception e){
//Exception handling
}
}
} // End of class ExecCommand |