我正在使用 Apache Commons Exec在我的项目中运行 bower操作系统(Linux 和 Windows)中的命令,这在 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) {
}
最佳答案
bower 是 JavaScript 文件,因此 bash 无法运行它。正确代码
if (isUnix()) {
commandline = new CommandLine("/usr/local/bin/node");
commandline.addArguments(new String[] { "/usr/local/bin/bower","--allow-root"}, false);
}
关于java - 在 Java 中执行 Linux nodejs 命令行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29832594/