java 从java执行.bat文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5090059/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Execute .bat file from java?
提问by user618111
i come back with my previous problem with executing .bat file from java program.
我回到了我之前从 Java 程序执行 .bat 文件的问题。
When i execute my java code, i don't understand why it's looking for my .bat file in the project directory of my Eclipse.
当我执行我的 java 代码时,我不明白为什么它在我的 Eclipse 的项目目录中寻找我的 .bat 文件。
I declare clearly the path like : "cmd.exe", "/C", "Start", "C:\\File\\batfile.bat"
If someone could explain me clearly, please.
Thank you very much!
我清楚地声明了路径,如:"cmd.exe", "/C", "Start", "C:\\File\\batfile.bat"
如果有人能清楚地解释我,请。非常感谢你!
i use win xp and Eclipse Helios.
我使用 win xp 和 Eclipse Helios。
here is my code:
这是我的代码:
String cmd;
try {
String[] command = { "cmd.exe", "/C", "Start", "C:\File\batfile.bat" };
Runtime r = Runtime.getRuntime();
Process p = r.exec(command);
p.waitFor();
} catch (Exception e)
{
System.out.println("Execution error");}
回答by lzap
The process cmd.exe (picked from your PATH environment variable) is created with the current working directory the same as in the parent process (eclipse.exe = java). That is most likely c:\eclipse or the workspace dir.
进程 cmd.exe(从您的 PATH 环境变量中选取)是使用与父进程(eclipse.exe = java)相同的当前工作目录创建的。这很可能是 c:\eclipse 或工作区目录。
If it cant find the file (C:\File\batfile.bat) it tries the current working dir. If you run this code using Run As Java try to change the working directory there. Also make sure the BAT file does exist.
如果它找不到文件 (C:\File\batfile.bat),它会尝试当前工作目录。如果您使用 Run As Java 运行此代码,请尝试更改那里的工作目录。还要确保 BAT 文件确实存在。
回答by user1676075
Try this instead:
试试这个:
String com = System.getEnv("ComSpec") != null ? System.getEnv("ComSpec") : "C:\Windows\System32\cmd.exe";
String[] command = { com, ...... }
ComSpec is often set to the path of cmd.exe. If not, use the full (expected path). You could also look for it in %SystemRoot%\system32. Or even %path%. But just checking ComSpec is better than using cmd.exe with nothing else by default.
ComSpec 通常设置为 cmd.exe 的路径。如果没有,请使用完整的(预期路径)。您也可以在 %SystemRoot%\system32 中查找它。甚至 %path%。但是只检查 ComSpec 比默认情况下使用 cmd.exe 更好。
As someone else pointed out, your default working directory when running from Eclipse is usually the Eclipse project folder.
正如其他人指出的那样,从 Eclipse 运行时的默认工作目录通常是 Eclipse 项目文件夹。
It's generally good practice not to rely on the working folder being anything useful. Instead specify paths to anything needed, or search the path (if the app doesn't do so for you).
不依赖于工作文件夹是任何有用的东西通常是一种很好的做法。而是指定任何需要的路径,或搜索路径(如果应用程序没有为您这样做)。
回答by user2519618
Remove Start
from the command - it's unnecessary - and try:
Start
从命令中删除- 这是不必要的 - 并尝试:
String[] command = { "cmd.exe", "/C", "C:\File\batfile.bat" };
回答by sschrass
Runtime.getRuntime().exec("cmd /c start C:\File\batfile.bat");
回答by sheel sindhu
To ensure the execution of a specific file, provide the complete pathinstead of just the executable name (which will only run from context of where the JVM was started - possibly unexpected within some IDE):
为了确保特定文件的执行,请提供完整的路径,而不仅仅是可执行文件的名称(它只会从 JVM 启动的上下文运行 - 在某些 IDE 中可能是意外的):
try{
Runtime.getRuntime().exec(".bat file complete path");
} catch(IOException e) {
System.out.println("exception");
}