简体   繁体   English

无法通过 Java 代码运行 bat 文件

[英]Couldn't run bat file through Java code

Problem is that the same code below is working on other machine with Windows 7. I also use Windows 7, and bat file works well.问题是下面的相同代码正在使用 Windows 7 的其他机器上运行。我也使用 Windows 7,并且 bat 文件运行良好。 But if I try to run this bat from code written before, cmd window just blink once and disappear.但是如果我尝试从之前编写的代码中运行这个蝙蝠,cmd window 只会闪烁一次然后消失。

s =  path + "makeInfomap.bat";  

try {   
    p = run.exec(s); 
} catch (Exception e) {
    System.out.println(e);  
    e.printStackTrace(); 
}  

final int exitVal = p.waitFor();

Run don't walk to this link: When Runtime.exec() won't .运行不要走到这个链接:当 Runtime.exec() 不会

It will tell you how to gobble output and error streams and will tell you how to call the OS's command interpreter when doing similar programs (although it is a little out of date).它将告诉您如何吞噬 output 和错误流,并会告诉您在执行类似程序时如何调用操作系统的命令解释器(尽管它有点过时)。

My guess is that java is calling it fine but that the batch file itself is running in to trouble.我的猜测是 java 调用它很好,但批处理文件本身遇到了麻烦。

Try adding a 'pause' as the last line of your batch file and see if the batch file's console gives you any usable information.尝试在批处理文件的最后一行添加“暂停”,看看批处理文件的控制台是否为您提供任何可用信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM