在单击jsp页面中的一个按钮时,我想运行一个批处理文件。我写这段代码是为了在方法中执行批处理文件,但它不起作用。请帮帮我。
public String scheduler() {
String result=SUCCESS;
try {
Process p = Runtime.getRuntime().exec("cmd /c start.bat", null, new File("C:\\Program Files\\MySQL\\MySQL Server 5.0\\bin\\start"));
System.out.println("manual scheduler for application.."+p);
} catch(Exception e) {
}
}发布于 2013-12-20 16:39:33
添加此代码,
batFile.setExecutable(true);
//Running bat file
Process exec = Runtime.getRuntime().exec(PATH_OF_PARENT_FOLDER_OF_BAT_SCRIPT_FILE+File.separator+batFile.getName());
byte []buf = new byte[300];
InputStream errorStream = exec.getErrorStream();
errorStream.read(buf);
logger.debug(new String(buf));
int waitFor = exec.waitFor();
if(waitFor==0) {
System.out.println("BAT script executed properly");
}发布于 2013-12-20 16:09:05
根据this的说法,以下代码应该可以工作(只需删除cmd /c即可):
public String scheduler() {
String result=SUCCESS;
try {
File f = new File("C:\\Program Files\\MySQL\\MySQL Server 5.0\\bin\\start")
Process p = Runtime.getRuntime().exec("start.bat", null, f);
System.out.println("manual scheduler for application.."+p);
} catch(Exception e) {
}
}发布于 2013-12-20 16:45:28
这里不清楚您是只想运行bat文件,还是想等待它运行。
//必填bat文件所在位置,例如K:/MyPath/MyBat.bat在我的例子中//如果bat文件在类路径中,那么您可以直接提供bat文件名MyBat.bat
**Runtime rt = Runtime.getRuntime() ;
Process batRunningProcess= rt.exec("cmd /c K:/MyPath/MyBat.bat");**//这是等待进程完成//如果进程完成,则value为0
**final int exitVal = lawTab_Indexer.waitFor();**//如果您只想运行bat文件,而不想等待它完成,则不需要该行。
https://stackoverflow.com/questions/20698890
复制相似问题