File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes I/O and Streams and the fly likes java program run external command on Unix Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "java program run external command on Unix " Watch "java program run external command on Unix " New topic
Author

java program run external command on Unix

Pierre Dubeau
Greenhorn

Joined: Feb 19, 2001
Posts: 7
Hi.
How to exit from this java program which run on Unix.
Help appreciate.
Pierre

here the complete code
import java.io.*;
public class RunCommand {
public static void main(String args[]) {
String s = null;
try {
// run the Unix "ps -ef" command
Process p = Runtime.getRuntime().exec("ps -ef");
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(p.getInputStream()));
//BufferedReader stdError = new BufferedReader(new
// InputStreamReader(p.getErrorStream()));
// read the output from the command
System.out.println("Here is the standard output of the command:\n");
while ((s = stdInput.readLine()) != null )
{
System.out.println(s);
}
// read any errors from the attempted command
//System.out.println("Here is the standard error of the command (if any):\n");
//while ((s = stdError.readLine()) != null) {
// System.out.println(s);
//}
stdInput.close();

System.exit(0);
}
catch (IOException e) {
System.out.println("exception happened - here's what I know: ");
e.printStackTrace();
System.exit(-1);
}
}
}
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: java program run external command on Unix