Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Java中的Angular CLI命令运行shell脚本_Java_Angular_Shell_Angular Cli - Fatal编程技术网

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

使用Java中的Angular CLI命令运行shell脚本,java,angular,shell,angular-cli,Java,Angular,Shell,Angular Cli,我正在尝试从Java运行shell脚本(使用Runtime.getRuntime().exec(cmd))。脚本文件中的所有命令似乎都正常运行,但angular cli(ng)命令除外 我的Java文件: System.out.println("Executing Script..."); final String[] cmd = new String[]{"/bin/bash", "test.sh"}; final Process process = Runtime.getRuntime().

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

我的Java文件:

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.");
test.sh:

#!/bin/bash
cd ~/ && 
ng new newAngularProject &&
输出:

Executing Script...
Script Executed.

不会抛出任何错误。所有其他命令都可以工作,但由于某种原因,我无法运行ng命令。此外,我还测试了从Java运行它的文件w/o——当我直接在控制台上运行相同的脚本时,它工作得非常好,所有命令(包括ng命令)都工作得很好。我正在MacOS上运行,以防您想知道。

同时打印错误流。如果有错误消息,您将得到它

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

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