服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

java调用系统执行程序(os command)


  以下代码在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);
}
}

扫描关注微信公众号