Spawning New Processes From a Java Program

The Java Runtime Environment (JRE) shields Java developers from the system level burdens of starting and stopping processes. A Java application runs inside the JVM, while a Java applet runs in a Web browser. In effect, both run inside the JVM (since the browser runs the applet in its own JVM). A standard Java application handles parallel tasks by running these tasks in independent threads.

However, most systems that support a Java runtime environment are capable of running several processes simultaneously. Java applications also are able to start new system programs calling the exec() method of the Runtime class. The exec() method has four forms:

 1. public Process exec(String cmd) throws IOException2. public Process exec(String[] cmds) throws IOException3. public Process exec(String cmd, String[] env) throws IOException4. public Process exec(String[] cmds, String[] env) throws IOException

The cmd parameter in Line 1 specifies the process to run. In Line 2, an array of String variables, cmds, is passed to the exec() method. The first String (cmd[0]) is the program name and the rest of the array strings are the arguments to the program. Lines 3 and 4 extend the methods in Lines 1 and 2 by allowing the Java program to supply environment variables in the array env.

Note that the exec() method results in the creation of a new child process. This process is a system process managed by the platform’s operating system that runs outside the JVM. The following example uses the exec() call shown on Line 4:

 1. Runtime rt = Runtime.getRuntime();2. String[] cmds = { "/bin/ls", "/" };3. String[] env = { "TERM=VT100" };4. Process p1 = rt.exec(cmds, env);

This code is equivalent to running the shell commands:

 $ set TERM=VT100$ /bin/ls /
Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS

homes in the real estate industry

Exploring the Latest Tech Trends Impacting the Real Estate Industry

The real estate industry is changing thanks to the newest technological advancements. These new developments — from blockchain and AI to virtual reality and 3D printing — are poised to change how we buy and sell homes. Real estate brokers, buyers, sellers, wholesale real estate professionals, fix and flippers, and beyond may