【发布时间】:2020-07-30 16:35:23
【问题描述】:
我有以下代码来执行 Openssl 命令并通过 Java Runtime 读取该命令产生的输出
public void executeCmd() throws IOException {
Runtime rt = Runtime.getRuntime();
String[] commands = new String[]{"openssl", "rsa", "-noout", "-modules", "-in", "myPathToKeyFile", "|", "openssl", "sha256"};
Process proc = rt.exec(commands);
BufferedReader stdInKey = new BufferedReader(new InputStreamReader(proc.getInputStream()));
String s = null;
while ((s = stdInKey.readLine()) != null) {
System.out.println(s);
}
}
当我通过 Cmd 运行命令时,它正在工作并且我能够看到输出
但是当我运行这段代码时,我收到以下错误:
Exception in thread "main" java.io.IOException: Cannot run program "openssl": CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
at java.lang.Runtime.exec(Runtime.java:620)
at java.lang.Runtime.exec(Runtime.java:485)
at com.renault.vnext.business.impl.CmdRunner.executeCmd(CmdRunner.java:22)
at com.renault.vnext.business.impl.CmdRunner.main(CmdRunner.java:10)
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:444)
at java.lang.ProcessImpl.start(ProcessImpl.java:140)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)
... 4 more
注意:我已经在路径变量中设置了 bin 文件夹
我可以在 cmd 中运行命令并获得输出。我的要求是从命令输出中获取标准输入值
非常感谢您的帮助!
【问题讨论】: