Java Runtime Exec Tutorial

Save

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 /\")
    GO

    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()
    GO
    InputStreamReader istrmrdr = new InputStreamReader(istrm)
    GO
    BufferedReader buffrdr = new BufferedReader(istrmrdr)
    GO

    String data
    GO
    while ((data = buffrdr.readLine()) != null) {
    System.out.println(data)
    GO
    }

    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.

Exiting

  • 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
    GO
    if (p.waitFor() != 0) {
    ev = p.exitValue()
    GO
    }

    A process can be killed using Process' destroy method

Considerations

  • 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.

Warnings

  • 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.

References

Promoted By Zergnet

Comments

Resources

You May Also Like

Related Searches

Check It Out

Geek Vs Geek: Robot battles, hoverboard drag race, and more

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