Created
January 25, 2010 19:59
-
-
Save rdegges/286186 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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