Skip to content

Instantly share code, notes, and snippets.

@rdegges
Created January 25, 2010 19:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rdegges/286186 to your computer and use it in GitHub Desktop.
Save rdegges/286186 to your computer and use it in GitHub Desktop.
/*
* Code hijacked and modified from: http://devdaily.com/java/edu/pj/pj010016/
*/
import java.io.*;
public class JavaRunCommand {
public static void main(String args[]) {
String s = null;
String binary = "C:\some\variable\binary\or\executable\path.ext";
try {
// run the binary / executable using the Runtime exec method
Process p = Runtime.getRuntime().exec(binary);
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);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment