Executing other system commands and programs from a Java Program

By Emiley J Viewed: 5620 times Emailed: 87 times Printed: 86 times Bookmark and Share



In safe environments, you can use Java to execute other heavyweight processes (that is, programs) on your multitasking operating system. Several forms of the exec( ) method allow you to name the program you want to run as well as its input parameters. The exec( ) method returns a Process object, which can then be used to control how your Java program interacts with this new running process. Because Java can run on a variety of platforms and under a variety of operating systems, exec( ) is inherently environment-dependent.

The following example uses exec( ) to launch notepad, Windows' simple text editor. Obviously, this example must be run under the Windows operating system.

// Demonstrate exec().
class ExecDemo {
public static void main(String args[]) {
Runtime r = Runtime.getRuntime();
Process p = null;
try {
p = r.exec("notepad");
} catch (Exception e) {
System.out.println("Error executing notepad.");
}
}
}

There are several alternate forms of exec( ), but the one shown in the example is the most common. The Process object returned by exec( ) can be manipulated by Process' methods after the new program starts running. You can kill the subprocess with the destroy( ) method. The waitFor( ) method causes your program to wait until the subprocess finishes. The exitValue( ) method returns the value returned by the subprocess when it is finished. This is typically 0 if no problems occur. Here is the preceding exec( ) example modified to wait for the running process to exit:

// Wait until notepad is terminated.
class ExecDemoFini {
public static void main(String args[]) {
Runtime r = Runtime.getRuntime();
Process p = null;
try {
p = r.exec("notepad");
p.waitFor();
} catch (Exception e) {
System.out.println("Error executing notepad.");
}
System.out.println("Notepad returned " + p.exitValue());
}
}

While a subprocess is running, you can write to and read from its standard input and output. The getOutputStream( ) and getInputStream( ) methods return the handles to standard in and out of the subprocess.




Comments(0)


Be the first one to add a comment

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:


Enter Code:
The Captcha image

Latest Tutorials

[2010-01-01]Converting properties using PropertyEditors and Other Spring features worth mentioning
[2010-01-01]How to create an array and method in JSP
[2010-01-01]inheritance in Java
[2010-01-01]Use List, Set, SortedSet, LinkedHashSet, Map, LinkedHashMap in java
[2010-01-01]cloneable in Java
[2010-01-01]Java program for Associate keys with values
[2010-01-01]Java code for Enumeration and using java.lang.reflect.Array
[2010-01-01]Insert an element in Array, Search and Sort Array by using java program
[2010-01-01]Type Casting in Java
[2010-01-01]How to initialize an Array and how to copy the array
[2009-10-15]TCP Server and TCP Client in Java
[2009-10-03]Simple java program to add an element to specified index of Java ArrayList
[2009-10-03]How to use set, get basic and nested properties for Spring framework
[2009-10-03]How to access instance from an inner class and accessing outer class variables in java
[2009-10-03]How to use and access the inner class in java

More Latest News


Most Viewed Articles (in last 30 days)
How to use ArrayList in Java
How to Send SMS using Java Program (full code sample included)
How to use Iterator in Java
XML and Java - Parsing XML using Java Tutorial
Using substring( ) in Java
FileReader and FileWriter example program in Java
indexOf( ) and lastIndexOf( ) in Java
wait(), notify() and notifyAll() in Java - A tutorial
HashMap example in Java
compareTo( ) in Java
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
Using StringTokenizer in Java
Transient vs Volatile modifiers in Java
Method Overloading (function overloading) in Java
instanceof sample program in Java
Most Emailed Articles (in last 30 days)
Components of program
How to Send SMS using Java Program (full code sample included)
XML and Java - Parsing XML using Java Tutorial
Execute system commands in a Java Program
Why java is important to the Internet
indexOf( ) and lastIndexOf( ) in Java
History of Object
FileReader and FileWriter example program in Java
Recursion in java
What is Java?
Sample Java Script that displays a movable clock
Overview of JavaScript Objects
compareTo( ) in Java
How to use ArrayList in Java
Executing Java Scripts