java Runtime.exec 运行 shell 脚本 - 无法打开文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17142573/
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
java Runtime.exec to run shell script - cannot open file
提问by user1400538
I am using Runtime.getRuntime().exec() to run a shell script from java code.
我正在使用 Runtime.getRuntime().exec() 从 java 代码运行 shell 脚本。
String[] cmd = {"sh", "build.sh", "/Path/to my/sh file"};
try{
Process proc = Runtime.getRuntime().exec( cmd );
}
catch(Exception e){
System.out.println("Exception is:"+e);
}
It gives me the following output in console:
它在控制台中给了我以下输出:
sh: Can't open build.sh
Am I following some wrong approach here? Cannot make out why his happens.
我在这里遵循一些错误的方法吗?无法弄清楚他为什么会发生。
EDIT
编辑
Based on the comment here, I have modified the String[] cmd = {"sh", "build.sh", "/Path/to my/sh file"};
to String[] cmd = {"sh", "/Path/to my/sh file/build.sh", "/Path/to my/sh file"};
. Now the problem is this script need to be executed from a particular path. When I execute this script from command prompt, I first change the directory to that path and execute it. How should I modify this code?
基于这里的评论,我已经修改了String[] cmd = {"sh", "build.sh", "/Path/to my/sh file"};
来String[] cmd = {"sh", "/Path/to my/sh file/build.sh", "/Path/to my/sh file"};
。现在的问题是这个脚本需要从特定路径执行。当我从命令提示符执行此脚本时,我首先将目录更改为该路径并执行它。我应该如何修改这段代码?
回答by fge
Use a ProcessBuilder
and set the working directory of the process to the directory where your script actually is:
使用 aProcessBuilder
并将进程的工作目录设置为脚本实际所在的目录:
final ProcessBuilder pb = new ProcessBuilder("/bin/sh", "script.sh", "whatever",
"arguments", "go", "here");
pb.directory(new File("/path/to/directory"));
// redirect stdout, stderr, etc
final Process p = pb.start();
See the ProcessBuilder
javadoc. It contains an example of what you can do. Runtime.exec()
is passé :p
请参阅ProcessBuilder
javadoc。它包含您可以做什么的示例。Runtime.exec()
过时了 :p
回答by Adam Siemion
sh
is unable to find the build.sh
script. To fix this you can provide the full path to build.sh
.
sh
无法找到build.sh
脚本。要解决此问题,您可以提供build.sh
.
回答by Juned Ahsan
Try this:
试试这个:
String[] cmd = {"sh build.sh", "/Path/to my/shfile"};
and better to use ProcessBuilder
最好使用 ProcessBuilder
ProcessBuilder pb = new ProcessBuilder("sh build.sh", "/Path/to my/shfile");
回答by Stephen C
The problem is that the "sh" command is unable to resolving the relative path "build.sh"
to an absolute path. The most likely explanation is that "build.sh"
is not in the current directory when you launch the command.
问题是“sh”命令无法将相对路径解析为"build.sh"
绝对路径。最可能的解释是"build.sh"
当您启动命令时它不在当前目录中。
Assuming that "/Path/to my/sh file"
string is the path to the "build.sh"
file, you need to run it like this:
假设"/Path/to my/sh file"
字符串是"build.sh"
文件的路径,你需要像这样运行它:
String[] cmd = {"/bin/sh", "/Path/to my/sh file/build.sh"};
try {
Process proc = Runtime.getRuntime().exec(cmd);
...
(.... or the equivalent using ProcessBuilder
)
(.... 或等效使用ProcessBuilder
)
On the other hand, if the "/Path/to my/sh file"
string is supposed to be an argumentto the "build.sh"
script, then you need to run it like this:
在另一方面,如果该"/Path/to my/sh file"
字符串应该是一个争论的"build.sh"
脚本,那么你需要这样运行:
String[] cmd = {"/bin/sh", "/some/dir/build.sh", "/Path/to my/sh file"};
try {
Process proc = Runtime.getRuntime().exec(cmd);
@fge's answer gives an alternative approach. He is settingthe current directory for the child process before it is launched. That is the correct solution for your updatedQuestion.
@fge 的回答提供了另一种方法。他在启动子进程之前设置它的当前目录。这是您更新的问题的正确解决方案。