2015-04-23 261 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); 
}