1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
|
String ScriptPath = "C:\\Users\\Desktop\\test.cmd";
String[] cmd = new String[2];
cmd[0] = ScriptPath;
cmd[1] = "TEST";
Runtime rt = Runtime.getRuntime();
try{
Process pr=rt.exec(cmd);
pr.waitFor(5,TimeUnit.SECONDS);
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(pr.getInputStream()));
BufferedWriter std=new BufferedWriter(new OutputStreamWriter(pr.getOutputStream()));
BufferedReader stdError = new BufferedReader(new
InputStreamReader(pr.getErrorStream()));
// read the output from the command
System.out.println("Here is the standard output of the command:\n");
String s = null;
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("ERROR");
}
} catch (Exception ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
} |
Partager