如何在Java中使用Runtime运行openssl命令?

huangapple go评论65阅读模式
英文:

How to run openssl command from JAVA using Runtime?

问题

我想读取使用PuttyGen生成的私钥和公钥,我使用openssl将它们转换为DER格式。

```java
String[] execStr = {"openssl","pkcs8", "-topk8", "-inform", "PEM", "-outform","DER", "-in", "src\\srcData\\openssh1\\privateKey.pem","-out", "src\\srcData\\openssh1\\privateKey.der" };

File execDir = new File("C:\\openssl-1.0.2d-fips-2.0.10\\bin");

Runtime.getRuntime().exec(execStr,null,execDir);

但是我得到了这个错误:

Exception in thread "main" java.io.IOException: Cannot run program "openssl" (in directory "C:\openssl-1.0.2d-fips-2.0.10\bin"): CreateProcess error=2, 系统找不到指定的文件
	at java.lang.ProcessBuilder.start(ProcessBuilder.java:1059)
	at java.lang.Runtime.exec(Runtime.java:631)
	at PrivateKeyReader.get(PrivateKeyReader.java:21)
	at Test1.main(Test1.java:50)
Caused by: java.io.IOException: CreateProcess error=2, 系统找不到指定的文件
	at java.lang.ProcessImpl.create(Native Method)
	at java.lang.ProcessImpl.<init>(ProcessImpl.java:455)
	at java.lang.ProcessImpl.start(ProcessImpl.java:151)
	at java.lang.ProcessBuilder.start(ProcessBuilder.java:1040)
	... 3 more

在这里,我无法找出确切的问题,如果有人知道,请告诉我。


<details>
<summary>英文:</summary>

I want to read private and public key generated using PuttyGen for that I used openssl to convert them into DER format.

String[] execStr = {"openssl","pkcs8", "-topk8", "-inform", "PEM", "-outform","DER", "-in", "src\srcData\openssh1\privateKey.pem","-out", "src\srcData\openssh1\privateKey.der" };

File execDir = new File("C:\openssl-1.0.2d-fips-2.0.10\bin");

Runtime.getRuntime().exec(execStr,null,execDir);


But I am getting this error:

Exception in thread "main" java.io.IOException: Cannot run program "openssl" (in directory "C:\openssl-1.0.2d-fips-2.0.10\bin"): CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1059)
at java.lang.Runtime.exec(Runtime.java:631)
at PrivateKeyReader.get(PrivateKeyReader.java:21)
at Test1.main(Test1.java:50)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(ProcessImpl.java:455)
at java.lang.ProcessImpl.start(ProcessImpl.java:151)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1040)
... 3 more


Here I am not able figure out the exact issue, please let me know if anyone know it.

</details>


# 答案1
**得分**: 0

我明白你已经下载了`openssl-1.0.2d-fips-2.0.10`的Windows版本。位于bin文件夹中的可执行文件被称为`openssl.exe`,而不是`openssl`。因此,你会遇到`系统找不到指定的文件`错误。因此,你的`execStr`应该是`String[] execStr = {&quot;openssl.exe&quot;, ...}`

为了防止将来出现这个问题,你可以让Windows资源管理器显示完整的`openssl.exe`文件名,而不是`openssl`,可以按照这里的说明进行操作:[这里](https://answers.microsoft.com/en-us/windows/forum/all/in-win10-how-to-show-the-file-extension-for/ed21ff20-cdb3-4263-9c7d-fc6ed125fc82)。

此外,请注意,当你将`C:\\openssl-1.0.2d-fips-2.0.10\\bin`作为`execDir`时,路径`src\\srcData\\openssh1\\privateKey.pem`会相对于`execDir`进行解释。因此,你应该将其转换为绝对路径,使用以下方法:
```java
File inputFile = new File(&quot;src\\srcData\\openssh1\\privateKey.pem&quot;);

String[] execStr = {&quot;openssl.exe &quot;, ..., &quot;-in&quot;, inputFile.getCanonicalPath(), ... };

同样的方法也适用于输出文件。

英文:

I understand you downloaded the Windows version of openssl-1.0.2d-fips-2.0.10. The executable contained in the bin folder is called openssl.exe, not openssl. For this reason, you get the error The system cannot find the file specified. Your execStr should therefore be String[] execStr = {&quot;openssl.exe&quot;, ...

To prevent this issue in the future, you can make Windows Explorer show the full openssl.exe name instead of openssl, using the instructions here.

Also note that when you use C:\\openssl-1.0.2d-fips-2.0.10\\bin as the execDir, the path src\\srcData\\openssh1\\privateKey.pem is interpreted relative to execDir. You should therefore turn it into an absolute path using:

File inputFile = new File(&quot;src\\srcData\\openssh1\\privateKey.pem&quot;);

String[] execStr = {&quot;openssl.exe &quot;, ..., &quot;-in&quot;, inputFile.getCanonicalPath(), ... };

The same also applies to the output file.

huangapple
  • 本文由 发表于 2020年4月7日 02:55:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/61066985.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定