我正在尝试使用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)有什么东西丢了吗?如何才能正确地捕获输出?
发布于 2016-03-23 18:57:51
有一点猜测,但可能会有所帮助。
有时exit code = 1代表“成功”。但是,Apache Commons Exec默认情况下会将exit code = 1解释为失败,并且如果有问题的脚本使用和exit code = 1退出,则会抛出ExecuteException。
您可以使用以下代码告诉DefaultExecutor "exit code = 1 = success“:
exec.setExitValue(1);可能不是原因,但值得一试。
https://stackoverflow.com/questions/36175326
复制相似问题