Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java program run external command on Unix

 
Pierre Dubeau
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
}
}
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic