【发布时间】:2016-03-23 10:15:56
【问题描述】:
我正在尝试使用 apache-commons-exec 运行一个脚本,该脚本是使用 java 近似来实现的。此脚本在生产服务器 (Linux) 中执行,但我需要在我的本地主机中对其进行测试以查看一切正常。
这是我启动 cygwin 的代码,该代码在 cmd.exe 中运行,但是当我尝试使用 commons.exec 启动它时它不起作用:
OutputStream outputStream = new ByteArrayOutputStream();
DefaultExecutor exec = new DefaultExecutor();
exec.setWatchdog(new ExecuteWatchdog(1000));
PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream);
exec.setStreamHandler(streamHandler);
CommandLine cmdLine = CommandLine.parse("C:\\cygwin64\\bin\\bash");
cmdLine.addArgument("-c");
cmdLine.addArgument("/cygdrive/c/dev/launch.sh");
int exit = exec.execute(cmdLine);
logger.warn("Job exit: " + exit);
它返回 1 并且没有输出或日志错误:
org.apache.commons.exec.ExecuteException: Process exited with an error: 1 (Exit value: 1)
有什么遗漏吗?如何正确捕获输出?
【问题讨论】:
-
... 在 Linux 环境中测试它。你的 bash 脚本可以有一个 she-bang #!/bin/bash 和粘性 uid 位 chmod s+u。 (windows cygwin 环境以 bash --login -i 启动)
-
不可能,谢谢。
标签: java apache shell apache-commons-exec