简体   繁体   中英

Execute Linux nodejs command line in Java

I am using Apache Commons Exec in my project to run bower command in OS (Linux and windows), this working great in windows but in Linux can't find the command "bower" and thank you for helping.

    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) {

    }

The bower is JavaScript file so the bash can't run it. Correct code

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM