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:

Here is tutorial example program testing exec() method:

 * RuntimeExec.java
 * Copyright (c) 2010, 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();
         InputStream procOut = proc.getInputStream();
         byte[] msgOut = new byte[64];
         int len = procOut.read(msgOut);
         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) {

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, HerongYang.com, All Rights Reserved.
   $len = read STDIN, $msgIn, 6;
   print STDOUT "Hello $msgIn!";
   close STDOUT;

When executed on my Windows 7 system with JDK 1.7.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.

Comparing to the same test with JDK 1.6.0 in 2010, the behavior of rt.exec() is not changed.

Last update: 2014.

Table of Contents

 About This Book

 Downloading and Installing JDK 1.8.0 on Windows

 Downloading and Installing JDK 1.7.0 on Windows

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 28.2.7 by Oracle Corporation

 JVM Runtime Data Areas

 Memory Management and Garbage Collectors

 Garbage Collection Tests

 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

 Outdated Tutorials


 PDF Printing Version