2015-04-23 168 views
1

我在我的项目中使用Apache Commons Exec在操作系统(Linux和Windows)中运行bower命令,这在Windows中工作良好,但在Linux中找不到命令“bower”并感谢您的帮助。在Java中执行Linux nodejs命令行

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

    } 
+0

当你从命令行(你的bash shell)执行'bower help'时会发生什么? – xerx593

+0

绝对它在终端工作,但是当我从Java调用没有创建它。 – Steve

+0

1.“你的终端”是“/ bin/bash”? 2.当你做'bash -c'bower --allow-root''时怎么办? – xerx593

回答

0

bower是JavaScript文件,因此bash无法运行它。正确的代码

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