使用Java中的Angular CLI命令运行shell脚本

时间:2018-06-11 13:40:32

标签: java angular shell angular-cli

我正在尝试从Java运行shell脚本(使用Runtime.getRuntime()。exec(cmd))。除了angular-cli(ng)命令之外,脚本文件中的所有命令似乎都正常运行。

我的Java文件:

stopProcessing

test.sh:

(tfenv)pi@raspberrypi:~/Downloads/opencv-3.3.0-source/build $ pip install tensorflow
Collecting tensorflow
  Could not find a version that satisfies the requirement tensorflow (from versions: )
No matching distribution found for tensorflow
(tfenv)pi@raspberrypi:~/Downloads/opencv-3.3.0-source/build $ pip --version
pip 10.0.1 from /home/pi/.conda/envs/tfenv/lib/python3.4/site-packages/pip (python 3.4)

Outout:

System.out.println("Executing Script...");
final String[] cmd = new String[]{"/bin/bash", "test.sh"};
final Process process = Runtime.getRuntime().exec(cmd);
process.waitFor();
final BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String s;
while ((s = reader.readLine()) != null) {
    System.out.println("Script output: " + s);
}
process.destroy();
System.out.println("Script Executed.");

不会抛出任何错误。所有其他命令都有效但由于某种原因,我无法运行命令。此外,我已经测试了没有从Java运行它的文件 - 当我直接在控制台上运行相同的脚本时,它运行良好,所有命令(包括ng命令)都可以正常工作。我正在使用MacOS,以防你想知道。

1 个答案:

答案 0 :(得分:0)

同时打印错误流。如果存在错误消息,您将收到错误消息。

    final BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
    while ((s = errorReader.readLine()) != null) {
        System.out.println("error: " + s);
    }

您也可以尝试在ng中使用test.sh的绝对路径,例如/ home / my / install / node-vxxx / ng,因为java生成的用于运行命令的进程可能无法获取您在.bashrc /.bash_aliases中设置的环境变量