1

I'm on a JSF application, I have a .bat that I want to launch when clicking on a Command button, this script is in the webcontent directory of my project. The code of the action is:

public String genererRapportTable() {
  try {
    ServletContext ctx = (ServletContext) FacesContext
      .getCurrentInstance().getExternalContext().getContext();
    String realPath = ctx.getRealPath("/");
    String[] command = { "cmd.exe", "/C", "start", realPath + "Tools\\cmd.bat" };
    Runtime.getRuntime().exec(command);
  } catch (IOException e) {
    e.printStackTrace();
  }
  return null;
}

The path is correctly constructed but the script is not launched!

The debugging screen:

enter image description here

This what I get for output :

enter image description here

So the path is correct but the script is not launched. When I put the .bat on c:\Tools and I use this path "C:\\Tools\\cmd.bat" it works. What is the problem?

4

2 回答 2

1

你能发布批处理文件的内容吗?我注意到实际路径被用作新命令提示符窗口的标题。这是滥用start命令的典型行为(第一个参数不是可执行文件的路径,而是title新窗口的路径。不要问我,我不知道为什么......)你start在批处理文件中使用吗?

于 2012-07-18T10:47:05.127 回答
1

如果您使用start运行批处理,则需要将带引号的标题作为第一个参数,因为您的路径包含空格并且也会被引用。
start 命令始终使用第一个引用的参数作为标题。

start "myTitle" cmd.exe /c myBat.bat

像这样的东西应该有效
String[] command = { "cmd.exe", "/C", "start", "\"myTitle\" \""+realPath + "Tools\\cmd.bat\""};

您还应该将 cmd.bat 重命名为更好的名称,这样它就不会与 cmd.exe 冲突。

于 2012-07-18T11:32:12.003 回答