英文:
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 = {"openssl.exe", ...}`
为了防止将来出现这个问题,你可以让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("src\\srcData\\openssh1\\privateKey.pem");
String[] execStr = {"openssl.exe ", ..., "-in", 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 = {"openssl.exe", ...
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("src\\srcData\\openssh1\\privateKey.pem");
String[] execStr = {"openssl.exe ", ..., "-in", inputFile.getCanonicalPath(), ... };
The same also applies to the output file.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论