在 Java 中执行 Linux nodejs 命令行

Execute Linux nodejs command line in Java

我在 OS(Linux 和 windows)中使用 Apache Commons Exec in my project to run bower 命令,这在 windows 中非常有效,但在 Linux 中找不到命令 "bower",感谢您的帮助。

    String command="bower --allow-root";
    CommandLine commandline = null;
    if (isWindows()) {
        commandline = new CommandLine("cmd");
        commandline.addArguments(new String[] { "/c", command }, false);
    }
    if (isUnix()) {
        commandline = new CommandLine("/bin/bash");
        commandline.addArguments(new String[] { "-c", command }, false);
    }
    ExecuteCommandResponse executeCommandResponse = new ExecuteCommandResponse();
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    DefaultExecutor exec = new DefaultExecutor();
    PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream);
    exec.setStreamHandler(streamHandler);
    exec.setWorkingDirectory(workingDirectory);
    try {
        exec.execute(commandline);
    } catch (Exception e) {

    }

凉亭是 JavaScript 文件,因此 bash 不能 运行 它。正确代码

if (isUnix()) {
    commandline = new CommandLine("/usr/local/bin/node");
    commandline.addArguments(new String[] { "/usr/local/bin/bower","--allow-root"}, false);
}