Java执行exe,bat等可执行文件

http://www.zxbc.cn/html/20080728/63755.html

 

Java执行exe,bat等可执行文件的实现代码:

Process proc = Runtime.getRuntime().exec(command);

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class CallExe {
public static void main(String[] args) {
String text = null;
String command = ""C://WINDOWS//NOTEPAD.exe"";//exe,bat文件名OR DOS命令
try {
Process proc = Runtime.getRuntime().exec(command);
BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
while ((text = in.readLine())!= null) {
System.out.println(text); //输出测试
}
} catch (IOException ioError) {
ioError.printStackTrace();
System.exit(0);
}
}

 

http://www.cujava.com/read.php?611

 

package RunExe; public class Runtime_Example { public static void main(String[] args) { try { Runtime rt = Runtime.getRuntime(); //(A) Calling a html from Java //1St Way //rt.exec("cmd.exe /C start C://j2sdk1.4.2_03//bin//nil.html"); /* 2nd Way String[] cmd = new String[4]; cmd[0] = "cmd.exe"; cmd[1] = "/C"; cmd[2] = "start"; cmd[3] = "C://j2sdk1.4.2_03//bin//nil.html"; */ //(B) Calling an .exe from java rt.exec("C://WINDOWS//NOTEPAD.exe"); } catch (Throwable t) { System.out.print(t.getMessage()); } } }

你可能感兴趣的:(JAVA)