FAQ

Java

JSP

Servlet


Advertisement



How to run external programs from java under window?

The java.lang.Runtime and java.lang.Process classes are available for executing and communicating with external programs. With an instance of the java.lang.Runtime class, it can execute an external program and return an instance of a subclass of java.lang.Process. The class Process provides methods for performing input from the process, performing output to the process, waiting for the process to complete, checking the exit status of the process, and destroying (killing) the process.

import java.io.*;
import static java.lang.System.out;
public class RumExternal {

public static void main(String[] args) {
String fileList="cmd /k dir";
try {
Runtime rt = Runtime.getRuntime ();

Process process = rt.exec (fileList);

BufferedReader br = new BufferedReader (
new InputStreamReader(
process.getInputStream()
)
);

String line;
while ((line = br.readLine ()) != null)
out.println (line);

br.close();

} catch (IOException e) {
e.printStackTrace();
}
}
}


Printer-friendly version Printer-friendly version | Send this 
article to a friend Mail this to a friend

Previous Next vertical dots separating previous/next from contents/index/pdf Contents

  |   |