以下代码在windows2000上可以运新哦。
sayhi.exe为任意的一个执行程序。
public static void main(string args[]) {
string s = null;
// system command to run
string cmd = "cmd /c c:/sayhi.exe";
// set the working directory for the os command processor
file workdir = new file("c://");
try {
process p = runtime.getruntime().exec(cmd, null, workdir);
int i = p.waitfor();
if (i == 0) {
bufferedreader stdinput =
new bufferedreader(
new inputstreamreader(p.getinputstream()));
// read the output from the command
while ((s = stdinput.readline()) != null) {
system.out.println(s);
}
} else {
bufferedreader stderr =
new bufferedreader(
new inputstreamreader(p.geterrorstream()));
// read the output from the command
while ((s = stderr.readline()) != null) {
system.out.println(s);
}
}
} catch (exception e) {
system.out.println(e);
}
}
闽公网安备 35060202000074号