Java调用系统执行程序(OS Command)
类别: JAVA教程
以下代码在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);
}
}
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);
}
}
- 上一篇: JavaVM,反射与动态代理
- 下一篇: javascript读取表单的值
-= 资 源 教 程 =-
文 章 搜 索