Java Runtime Exec Tutorial


Java's Runtime class provides access to the runtime environment in which the application is running. Runtime's exec methods allow you to run external programs from a Java application. Executing external applications within a Java application allows you to open a web page in a browser, interact with operating system utilities or launch a console window.

Get the Runtime Environment

  • All Java applications have an instance of the runtime environment. Use Runtime's getRuntime() method to access this object. For example:

    Runtime rt = Runtime.getRuntime();

Execute an External Application

  • Runtime offers several ways to call the exec method, which returns a Process object. The simplest way to execute an external application is the Runtime method exec(String command) , where \"command\" is the command line that executes the application. The following call executes the \"ls\" command to get a directory listing for the root directory on a Linux or Unix system:

    Process p = rt.exec(\"ls /\")

    The exec method can also be called with an array containing the command and its arguments, environment variable settings and working directory.

Get Process Information

  • The Process class provides access to the input stream, output stream, error stream and exit value of the executed process.

    The Process.getInputStream method provides access to the process' standard output stream through your application's input stream. The stream should be buffered. Continuing the previous example, the following code accesses the output from the \"ls /\" command prints out the results.

    InputStream istrm = p.getInputStream()
    InputStreamReader istrmrdr = new InputStreamReader(istrm)
    BufferedReader buffrdr = new BufferedReader(istrmrdr)

    String data
    while ((data = buffrdr.readLine()) != null) {

    Similarly, you can capture the error stream by using Process' getErrorStream and using a buffered input stream to handle the data. Your application can also send data to the process' input stream via your application's output stream. Use Process' getOutputStream method and buffer the output stream.


  • Capture the exit value of the process using Process' exitValue method to determine if it exited normally. Ensure the process has exited using Process' waitFor method to cause the current thread to wait for the process to exit. For example, capture the exit value in a variable, which can be used later to make decisions about how your application will proceed:

    int ev = 0
    if (p.waitFor() != 0) {
    ev = p.exitValue()

    A process can be killed using Process' destroy method


  • Java 5 introduced the ProcessBuilder class to replace Runtime.exec. ProcessBuilder makes it easier to customize the process, and provides better control over starting the process.


  • One of the primary features of the Java programming language is its portability to multiple computing platforms. Calling external processes that are specific to a particular operating system keeps your application from being portable. Avoid using Runtime.exec when a method for accomplishing the same goal exists in the Java language.

Related Searches


Promoted By Zergnet



You May Also Like

Related Searches

Is DIY in your DNA? Become part of our maker community.
Submit Your Work!