Java 更改目录并在同一命令提示符下运行批处理文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/544519/
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
change directory and run the batch file in the same command prompt
提问by user48094
Is there any way to open the command prompt and change directory in the command prompt and run the batch file in the same command prompt using java.
有什么方法可以打开命令提示符并更改命令提示符中的目录并使用 java.lang.
I know how to open the command prompt using java. Thanks,
我知道如何使用 java 打开命令提示符。谢谢,
回答by Lawrence Dol
You can encode the CD and the batch file in the value for cmd.exe /K. From the doco (cmd /?):
您可以在 cmd.exe /K 的值中对 CD 和批处理文件进行编码。来自 doco (cmd /?):
Note that multiple commands separated by the command separator '&&' are accepted for string if surrounded by quotes.
请注意,如果用引号括起来,字符串接受由命令分隔符 '&&' 分隔的多个命令。
For Example:
例如:
cmd /C "CD C:\ && Dir"
cmd /C "CD C:\Windows && Dir"
cmd /C "CD C:\Windows && MySuperSuperBatchFile"
For more detail, run:
有关更多详细信息,请运行:
cmd /?
from the command line.
从命令行。
回答by Charlie Martin
It's difficult to do from Java for goofy platform-independence reasons: basically what if you're running java on a system that doesn't have hierarchical directories?
由于愚蠢的平台独立性原因,从 Java 很难做到:基本上,如果您在没有分层目录的系统上运行 java 怎么办?
There are a number of workarounds depending on what you're really trying to do, but possibly the simplest is to run the eventual command using
java.lang.Runtime.exec()
.
根据您真正尝试做的事情,有多种解决方法,但最简单的方法可能是使用
java.lang.Runtime.exec()
.
.
.
回答by Murthy
Keep the batch file in "path". You can execute it without going to any specific directory.
将批处理文件保存在“路径”中。您可以在不转到任何特定目录的情况下执行它。
(For example you can have an entry "set path=%path%;C:..........\YourBatchFile.bat" in Autoexec.bat in Windows environment)
(例如,您可以在 Windows 环境中的 Autoexec.bat 中有一个条目“set path=%path%;C:.........\YourBatchFile.bat”)
回答by Ron
get the environment var "comspec" then exec %comspec% /c start/d directory /b batchfile.bat
获取环境变量 "comspec" 然后 exec %comspec% /c start/d directory /b batchfile.bat
回答by hlovdal
A couple of the java.lang.Runtime.exec()variations does have a dir argument, so I assume you are not thinking of that?
几个java.lang.Runtime.exec()变体确实有一个 dir 参数,所以我假设您没有想到这一点?
You can compile the following C program and execute as a wrapper to start any program in any directory you want. If you use a String array with Runtime.exec you will avoid all issues of command line parsing/portability/proper quoting of the arguments.
您可以编译以下 C 程序并作为包装器执行以在您想要的任何目录中启动任何程序。如果您将 String 数组与 Runtime.exec 一起使用,您将避免命令行解析/可移植性/正确引用参数的所有问题。
I do not have any windows machine to test on here, but if you compile the C program to cdexe.exe you should be able to use it as the following:
我没有任何 Windows 机器可以在这里测试,但是如果您将 C 程序编译为 cdexe.exe,您应该可以如下使用它:
public class Main {
public static void main(String args[]) {
String[] s = { "c:\some\place\cdexe.exe",
"c:\start\dir", "c:\my\batch\file.bat", "arg1", "..." };
try {
java.lang.Runtime.getRuntime().exec(s);
} catch (java.io.IOException e) {
e.printStackTrace();
}
}
}
I guess unistd.h is maybe not available on windows, but just substitute with one containing a execv prototype.
我猜 unistd.h 可能在 Windows 上不可用,但只需用包含 execv 原型的替代。
#include <stdio.h>
#include <unistd.h> // or hard code "int execv(const char *path, char *const argv[]);"
int main(int argc, char *argv[])
{
if (argc < 3) {
fprintf(stderr, "Error: Usage: %s <directory> <program> [arguments]\n", argv[0]);
return 1;
}
if (chdir(argv[1]) < 0) {
perror("Error");
fprintf(stderr, "chdir(%s) failed\n", argv[1]);
return 1;
}
argv[1] = argv[2];
execv(argv[1], &argv[2]); // use execvp if you want PATH to be searched
perror("Error");
fprintf(stderr, "execv returned\n");
return 0;
}
回答by itsadok
Be wary of Java's exec. It can hang if the batch process fills the output buffer, and cause other weird problems.
小心 Java 的 exec。如果批处理填满输出缓冲区,它可能会挂起,并导致其他奇怪的问题。
I suggest you look at apache exec. Specifically for your needs you should note that the Executor interface has a setWorkingDirectorymethod.
我建议你看看 apache exec。专门针对您的需求,您应该注意 Executor 接口有一个setWorkingDirectory方法。
Basic usage:
基本用法:
Executor exec = new DefaultExecutor();
exec.setWorkingDirectory(new File("C:\My\Dir\"));
CommandLine cl = new CommandLine("mybatch.bat");
int exitvalue = exec.execute(cl);