在执行.bat文件作为独立时,我可以调用它,但是当我将其公开为REST WEBS服务时,我会发现错误,因为找不到 - 找不到bat文件。P>
如何设置蝙蝠文件执行的路径?
@GET
@Path("/triggerMyBat")
public Response triggerMyBat() throws Exception {
//public static void main(String[] args){
try {
String ss = null;
Process p = Runtime.getRuntime().exec("cmd.exe /c start myBat.bat");
BufferedWriter writeer = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
writeer.write("dir");
writeer.flush();
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
while ((ss = stdInput.readLine()) != null) {
System.out.println(ss);
}
while ((ss = stdError.readLine()) != null) {
System.out.println(ss);
}
} catch (Exception e) {
System.out.println(e);
} finally {
System.out.println("ok");
}
return Response.status(200).entity("Success").build();
}
如果您的蝙蝠文件位于cmd.exe引用的文件夹中,则可以调用它。否则,您需要将完整的路径设置为文件或将BAT的文件夹放在Windows环境变量的路径变量中。