【发布时间】:2016-09-03 17:06:54
【问题描述】:
您好,我在 Java 和 Spring 框架中使用 openssl 生成证书和密钥,
以下是我的代码
String[] cmds = new String[4];
cmds[0] = String.format("openssl genrsa -out %s.key 2048", path+name);
cmds[1] = String.format("openssl req -new -key %s.key -out %s.csr", path+name, path+name);
cmds[2] = String.format("openssl x509 -req -in %s.csr -CA %s.pem -CAkey %s.key -CAcreateserial -out %s.crt -days 365 -sha512 -extensions mysection -extfile conf.cnf", path+name, path+rootName, path+rootName, path+name);
cmds[3] = String.format("openssl pkcs12 -export -out %s.pfx -inkey %s.key -in %s.crt", path+name, path+name, path+name);
Runtime r = Runtime.getRuntime();
Process p = null;
try {
p = r.exec(cmds);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
作为回报,我得到了关注
java.io.IOException:无法运行程序“openssl genrsa -out /home/lalani/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/api-server/WEB-INF/applications/certificate/Riksof.key 2048": error=2, 没有这样的文件或目录
请指导我哪里做错了?
还请建议我如何在异步任务中运行这个Runtime.getRuntime().exec()?
【问题讨论】:
-
错误很简单,没有找到key。为什么不尝试一个简单的钥匙位置。你是在 linux 上,那么简单的 /home/username/Riksof.key。在异步任务中运行它是什么意思。你运行它的方式已经是异步的,当linux命令执行时,你不知道它什么时候结束,除非你调用waitFor()方法。
-
这里的
path和name是什么?您是否经过正确的位置? -
是的,上面的错误
/home/lalani/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/api-server/WEB-INF/applications/certificate/是路径,Riksof是名称 -
这也不是问题,因为从技术上讲,此文件不存在此命令
openssl genrsa -out Riksof.key 2048旨在在当前位置生成名称为Riksof.key的密钥文件 -
我也可以使用 cd 访问此目录
/home/lalani/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/api-server/WEB-INF/applications/certificate/,因此该目录确实存在
标签: java spring process openssl runtime