【发布时间】:2015-04-23 19:24:48
【问题描述】:
我在我的项目中使用Apache Commons Exec 在操作系统(Linux 和 Windows)中运行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) {
}
【问题讨论】:
-
当您从命令行(您的 bash shell)执行
bower help时会发生什么? -
绝对可以在终端中工作,但是当我从 Java 调用时没有找到它。
-
1.“你的终端”是“/bin/bash”? 2.
bash -c 'bower --allow-root'的时候怎么办? -
它可以工作
# bash -c 'bower --allow-root' Usage: bower <command> [<args>] [<options>] ..... -
我的意思是在终端中我通过 SSH 连接到我的服务器并运行命令