![](/img/trans.png)
[英]Different output — when running mathtext in command line and when the command is executed from a java program using apache-commons-exec
[英]Apache commons command line execute method is returning exit code -1 when running in tomcat application and throwing ExecuteException
使用 Aapche commons CommandLine 库,我试图在 SLES OS 中运行于 tomcat 的 web 应用程序中执行 openssl 命令。 下面是代码片段 -
CommandLine cmd = new CommandLine("/usr/bin/openssl")
.addArgument("genrsa")
.addArgument("-out")
.addArgument("/root/testcert_tomcat.key")
.addArgument("4096");
logger.info("Command - "+ cmd.toString());
DefaultExecutor executor = new DefaultExecutor();
executor.setProcessDestroyer(new ShutdownHookProcessDestroyer());
executor.setStreamHandler(new PumpStreamHandler(null, null, null));
executor.setWatchdog(new ExecuteWatchdog(60000));
executor.setExitValue(0);
try {
int exitValue = executor.execute(cmd);
if(exitValue != 0 ){
logger.info("Exit value - "+ exitValue);
}
} catch (IOException ex) {
logger.info("Exception - "+ ex.getMessage());
return false;
}
命令 - openssl genrsa -out test.key 4096
当从 tomcat web 应用程序外部的正常 java 代码或直接从 shell 命令行运行时,相同的命令工作正常。 如果有人有任何想法,请提出可能是什么问题。
捕获到异常 - org.apache.commons.exec.ExecuteException:进程因错误退出:1(退出值:1)
不太确定,但我尝试检查 tomcat 用户,它以 root 身份运行 -
libv222:/usr/share/tomcat/webapps # ps auxwww | grep -v grep | grep tomcat
tomcat 13700 27.7 12.3 3610620 498412 ? Ssl 20:20 0:46 /usr/bin/java -Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=5000,suspend=n -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/tmp -Djava.security.auth.login.config=/usr/share/tomcat/conf/jaas.config -Djava.library.path=/lib/emc/powerpath/remote_tools/latest/ -XX:MaxPermSize=128M -Xms512m -Xmx1536m -classpath /usr/share/tomcat/bin/bootstrap.jar:/usr/share/tomcat/bin/tomcat-juli.jar:/usr/share/java/commons-daemon.jar -Dcatalina.base=/usr/share/tomcat -Dcatalina.home=/usr/share/tomcat -Djava.endorsed.dirs= -Djava.io.tmpdir=/var/cache/tomcat/temp -Djava.util.logging.config.file=/usr/share/tomcat/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager org.apache.catalina.startup.Bootstrap start
root 16591 0.0 0.0 4244 728 pts/0 S+ 17:34 0:00 tail -f /var/log/tomcat/catalina.out
下面的代码对我有用,我必须从 Apache commons 切换到创建一个进程才能使其工作。 看起来我们需要在新进程中执行此命令才能成功执行。
人们在评论中建议目录应该在任何需要的地方具有读写访问权限,这绝对是命令执行所必需的。
private static boolean executeOpenSslCmdProcess(String host) {
StringBuffer command = new StringBuffer();
command.append("openssl x509 -req -in /tmp/ppserver.csr -out /tmp/ppserver.crt -sha256 -CA /tmp/powerpath-CA.crt -CAkey /tmp/powerpath-CA.key -CAcreateserial -days 821");
String[] cmd = {
"/bin/sh",
"-c",
command.toString()
};
try {
Process process = Runtime.getRuntime().exec(cmd);
InputStream inputStream = process.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, CHAR_SET));
result = br.lines().collect(Collectors.joining(System.lineSeparator()));
exitCode = process.waitFor();
logger.info("ExitCode : {} ", exitCode);
if (exitCode != 0) {
return false;
}
} catch (IOException ex) {
logger.info("IOException - " + ex);
return false;
} catch (InterruptedException ex) {
logger.info("InterruptedException - " + ex);
}
}
感谢评论中的建议!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.