简体   繁体   English

java Runtime.exec运行shell脚本 - 无法打开文件

[英]java Runtime.exec to run shell script - cannot open file

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"}; 根据这里的评论,我修改了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"}; 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? 我应该如何修改此代码?

Use a ProcessBuilder and set the working directory of the process to the directory where your script actually is: 使用ProcessBuilder并将流程的工作目录设置为脚本实际所在的目录:

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 . 请参阅ProcessBuilder javadoc It contains an example of what you can do. 它包含了您可以执行的操作的示例。 Runtime.exec() is passé :p Runtime.exec()已通过:p

sh is unable to find the build.sh script. sh无法找到build.sh脚本。 To fix this you can provide the full path to build.sh . 要解决此问题,您可以提供build.sh的完整路径。

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"); 

The problem is that the "sh" command is unable to resolving the relative path "build.sh" to an absolute path. 问题是“sh”命令无法将相对路径"build.sh"解析为绝对路径。 The most likely explanation is that "build.sh" is not in the current directory when you launch the command. 最可能的解释是,当您启动命令时, "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 argument to 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. @fge的答案提供了另一种方法。 He is setting the current directory for the child process before it is launched. 他正在为子进程启动之前设置当前目录。 That is the correct solution for your updated Question. 这是您更新的问题的正确解决方案。

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

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