源命令无法通过 Java 工作

Beh*_*der 5 python java bash terminal

从昨天开始,我一直在尝试使用 JAVA 在终端 (MAC) 上执行命令,但无论我做什么都不工作。

我有以下 2 个命令要执行并在 JAVA 中获取输出

source activate abc_env
python example.py
Run Code Online (Sandbox Code Playgroud)

到目前为止,我已经尝试了以下方法而没有任何输出

String[] command = new String[] { "source activate abc_env", "python example.py"};
String result = executeCommands(command);
Run Code Online (Sandbox Code Playgroud)

这是我的 executeCommands 方法

private static String executeCommands(String[] command) {

        StringBuffer output = new StringBuffer();

        Process p;
        try {
            for(int i=0; i< command.length;i++)
            {
                p = Runtime.getRuntime().exec(command[i]);
                p.waitFor();
                BufferedReader reader = 
                                new BufferedReader(new InputStreamReader(p.getInputStream()));

                String line = "";           
                while ((line = reader.readLine())!= null) {
                    output.append(line + "\n");
                }
                System.out.println("Error output: " + p.exitValue());
                System.out.println("Output:" + output);

            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Here");
        }
        return output.toString();
    }
Run Code Online (Sandbox Code Playgroud)

这给了我以下异常

无法运行程序“源”:错误=2,没有那个文件或目录

我在网上搜索,人们说源不会像这样工作,我应该将命令更改为

String[] command = new String[] { "bash -c 'source activate abc_env'", "python example.py"};
Run Code Online (Sandbox Code Playgroud)

现在,我没有收到异常,但该命令仍然不起作用,它返回 '2' 作为exitValue()

然后我尝试将命令作为脚本执行

#!/bin/bash
source activate abc_env
python example.py
Run Code Online (Sandbox Code Playgroud)

当我将 .sh 文件作为字符串读取并将其传递给命令时,出现以下异常

无法运行程序 "#!/bin/bash": error=2, No such file or directory

所以,我的问题是如何通过 Java 正确运行源命令后跟 python 命令?我的最终目标是从 Java 执行一些 python。

EDIT1:如果我尝试以下命令并打印输出流

String[] command = {
                "/bin/bash",
                "-c",
                "source activate cvxpy_env"
        };

executeCommand(command));
Run Code Online (Sandbox Code Playgroud)

输出流:

退出值:1

ErrorOutput:/bin/bash: activate: 没有那个文件或目录

如果我尝试相同的命令,但在 'source activate abc_env' 周围加上单引号。我得到以下输出

退出值:127

ErrorOutput:/bin/bash: source activate cvxpy_env: command not found

解决方案:

String[] command = {
                "/bin/bash",
                "-c",
                "source /Users/pc_name/my_python_library/bin/activate abc_env;python example.py"
        };
Run Code Online (Sandbox Code Playgroud)

Ser*_*nov 4

根据 Javadoc,Runtime.exec(String)使用 a 将命令分解为命令参数列表StringTokenizer,这可能会将命令分解为:

bash
-c
'source
activate
abc_env'
Run Code Online (Sandbox Code Playgroud)

这显然不是你想要的。Runtime.exec(String[])您应该做的可能是使用接受现成参数列表的版本,并将其传递给它new String[] {"bash", "-c", "source activate abc_env"}

现在,要了解它不起作用的原因,您不仅应该阅读它stdout,还应该stderr使用 阅读p.getErrorStream()。只需打印出您所读到的内容,这将是一个很好的调试帮助。

回复:您的编辑。现在看来,就 Java 和 bash 而言,它工作得很好。输出“激活:没有这样的文件或目录”可能是命令成功运行的输出source。只是source找不到该activate文件。它在工作目录中吗?如果没有,你可能应该有"cd /wherever/your/files/are; source activate cvxpy_env"。哦,如果你的 python 脚本依赖于 source 命令的任何副作用,你可能必须在同一个命令中执行它bash实例中执行它,即:

String[] command = {
                "/bin/bash",
                "-c",
                "cd /wherever/your/files/are && source activate cvxpy_env && python example.py"
        };
Run Code Online (Sandbox Code Playgroud)

或者更好的是,将其全部打包到一个 shell 脚本中,然后就可以了Runtime.exec("./my_script.sh")(不过,不要忘记chmod +x)。