Controlling The Command Line with Java
I wrote this to launch some batch files from within an Java program.
public class CmdExec {
// process invoking program will call
private Process p;
// empty constructor
public CmdExec() { }
// execute command
public void exe(String cmdline) {
try {
// string for system out
String line;
// create process
p = Runtime.getRuntime().exec(cmdline);
// capture output stream of program
BufferedReader input = new BufferedReader(new InputStreamReader(p
.getInputStream()));
// get all lines of output
while ((line = input.readLine()) != null) {
System.out.println(line);
}
// close input stream
input.close();
} catch (Exception e) {
e.printStackTrace();
}
}
// ability to kill process
public void kill() {
p.destroy();
}
}
Did you enjoy this post? Why not leave a comment below and continue the conversation, or subscribe to my feed and get articles like this delivered automatically to your feed reader.
Comments
No comments yet.
Leave a comment