Created
September 30, 2016 14:35
-
-
Save olehermanse/86a0499f1f50d2d657e634d70929f087 to your computer and use it in GitHub Desktop.
This file contains 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
import java.io.*; | |
public class JavaRunCommand { | |
public static void main(String args[]) { | |
String s = null; | |
String navn = "Jeanette"; | |
String nummer = "99998888"; | |
try { | |
Process p = Runtime.getRuntime().exec("cowsay Hei mitt navn er " + navn + " og mitt nr er "+ nummer+"."); | |
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