Logo 
Search:

Java Answers

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds
  Question Asked By: Bill Howell   on Jul 02 In Java Category.

  
Question Answered By: Alberta Miller   on Jul 02

I found this code  on the web. I believe it is what you are looking for?

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");
Process p = Runtime.getRuntime().exec("date");

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);
}

System.exit(0);
}
catch (IOException e) {
System.out.println("exception happened - here's what I know: ");
e.printStackTrace();
System.exit(-1);
}
}
}



// Copyright 1998 DevDaily Interactive, Inc. All Rights Reserved.

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");
Process p = Runtime.getRuntime().exec("date");

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);
}

System.exit(0);
}
catch (IOException e) {
System.out.println("exception happened - here's what I know: ");
e.printStackTrace();
System.exit(-1);
}
}
}

Share: 

 

This Question has 3 more answer(s). View Complete Question Thread

 
Didn't find what you were looking for? Find more on Running an executable Or get search suggestion and latest updates.


Tagged: