JVM Tutorials - Herong's Tutorial Examples
Dr. Herong Yang, Version 4.10

Executing System Commands

This section provides a tutorial example on how to execute system commands as separate processes outside the JVM instance.

The JVM instance also allows you to execute a system command as a separate process by using the exec() method. The separate process can be managed these methods:

  • getOutputStream() - Returns an output stream representing the standard input to the process.
  • getInputStream() - Returns an input stream representing the standard output from the process.
  • waitFor() - Puts the current thread on hold until the process is terminated.

Here is tutorial example program testing exec() method:

/**
 * RuntimeExec.java
 * Copyright (c) 2010 by Dr. Herong Yang, herongyang.com
 */
import java.io.*;
class RuntimeExec {
   public static void main(String[] a) {
      PrintStream out = System.out;
      Runtime rt = Runtime.getRuntime();
     
      try {
         out.println("Executing a command in a separate process...");
         Process proc = rt.exec("perl HelloSleep.pl");
         
         OutputStream procIn = proc.getOutputStream();
         procIn.write("Herong".getBytes());
         procIn.close();
   
         InputStream procOut = proc.getInputStream();
         byte[] msgOut = new byte[64];
         int len = procOut.read(msgOut);
         procOut.close();
         out.println("Output from the command: "
            +new String(msgOut,0,len));
   
         out.println("Waiting for the process to finish...");
         int rc = proc.waitFor();
         out.println("Status code returned by the process "+rc);
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}

The Perl program, HelloSleep.pl, is simple, reading 6 characters from the standard input, writing a hello message to the standard output, then sleep forever:

#
#- HelloSleep.pl
#- Copyright (c) 2010 by Dr. Herong Yang, herongyang.com
#
   $len = read STDIN, $msgIn, 6;
   print STDOUT "Hello $msgIn!";
   close STDOUT;
   sleep;

When executed on my Windows XP system with JDK 1.6.0, I got this result:

C:\herong\jvm>java RuntimeExec
Executing a command in a separate process...
Output from the command: Hello Herong!
Waiting for the process to finish...
(terminate the Perl process using Windows Task Manager)
Status code returned by the process 1

The test result confirms that the process invoked by the exec() method is running outside the JVM instance.

Last update: 2010.

Table of Contents

 About This Book

 Download and Install Java SE 1.6 Update 2

java.lang.Runtime Class - The JVM Instance

 What Is Runtime?

 Printing Runtime Basic Information

 Running the Garbage Collector Explicitly

 Shutting Down or Terminating the JVM Instance

Executing System Commands

 Loading Native Libraries

 java.lang.System Class - The Operating System

 ClassLoader Class - Class Loaders

 Class Class - Class Reflections

 Sun's JVM - Java HotSpot VM

 JRockit JVM 7.0 by BEA Systems

 JRockit JVM 8.0 by BEA Systems

 Memory Management Rules and Tests

 Garbage Collection Tests

 Stack Overflow Tests

 Thread Testing Program and Result

 StringBuffer Testing Program and Result

 CDS (Class Data Sharing)

 Micro Benchmark Runner and JVM Options

 Micro Benchmark Tests on "int" Operations

 Micro Benchmark Tests on "long" Operations

 Micro Benchmark Tests in JIT Compilation Mode

 Micro Benchmark Tests on "float" and "double" Operations

 References

 PDF Printing Version

Dr. Herong Yang, updated in 2010
Executing System Commands