Skip to content

Instantly share code, notes, and snippets.

@olehermanse
Created September 30, 2016 14:35
Show Gist options
  • Save olehermanse/86a0499f1f50d2d657e634d70929f087 to your computer and use it in GitHub Desktop.
Save olehermanse/86a0499f1f50d2d657e634d70929f087 to your computer and use it in GitHub Desktop.
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