Home Page

Tips page
c
cellulari
debian
egittologia
emacs
emacs-latex
hardware
html
inglese
java

*Execute a command
*The correct way to use process.waitFor() method

latex
linux
matlab
misc
mysql
network
octave
programming
python
security
sed
tech
webapps
windows

University Page

Programming

Debian & Linux

Some works

About me

Del.icio.us Bookmarks

BOINC Combined Statistics

Site Statistics

Contact me sending an e-mail (antispam defense activated)

debian

hacker emblem

blogger

GeoURL

View Sandro Tosi's profile on LinkedIn

This is my Google PageRank

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