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 interacted with these methods:

Here is tutorial example program testing exec() method:

/* RuntimeExec.java
 * Copyright (c) HerongYang.com. All Rights Reserved.
 */
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) HerongYang.com. All Rights Reserved.
#
   $len = read STDIN, $msgIn, 6;
   print STDOUT "Hello $msgIn!";
   close STDOUT;
   sleep;

When executed with JVM in JDK, I got this result:

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

Table of Contents

 About This Book

 JVM (Java Virtual Machine) Specification

 Java HotSpot VM - JVM by Oracle/Sun

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

 JVM Runtime Data Areas

 JVM Stack, Frame and Stack Overflow

 Thread Testing Program and Result

 CPU Impact of Multi-Thread Applications

 I/O Impact of Multi-Thread Applications

 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

 OpenJ9 by Eclipse Foundation

 JRockit JVM 28.2.7 by Oracle Corporation

 Archived Tutorials

 References

 Full Version in PDF/EPUB