源命令无法通过 Java

Source command not working through Java

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

我有以下 2 个命令要执行并在 JAVA

中取回输出
source activate abc_env
python example.py

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

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

这是我的 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();
    }

这给了我以下异常

Cannot run program "source": error=2, No such file or directory

我在网上搜索了一下,有人说 source 不会像这样工作,我应该将命令更改为

String[] command = new String[] { "bash -c 'source activate abc_env'", "python example.py"};

现在,我没有得到异常,但命令仍然不起作用,它 returns '2' as exitValue()

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

#!/bin/bash
source activate abc_env
python example.py

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

Cannot run program "#!/bin/bash": error=2, No such file or directory

所以,我的问题是如何通过 Java 运行 source 命令后接 python 命令?我的最终目标是从 Java.

执行一些 python

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

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

executeCommand(command));

输出流:

ExitValue:1

ErrorOutput:/bin/bash: activate: No such file or directory

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

ExitValue: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"
        };

尝试

String[] command = {
                "/bin/bash",
                "-c",
                "source activate abc_env; " + "python example.py"
        };

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

bash
-c
'source
activate
abc_env'

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

现在,要了解它为什么不起作用,您不仅应该阅读它的 stdout,还应该使用 p.getErrorStream() 阅读 stderr。只需将您阅读的内容打印出来,这将是一个很好的调试辅助工具。

回复:您的编辑。就 Java 和 bash 而言,现在它看起来工作正常。输出 "activate: No such file or directory" 可能是 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"
        };

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