2

我想通过在openSSL文件夹的bin目录中运行命令。我想从 C:\OpenSSL-Win32\bin\ 目录运行命令。我需要执行以下命令:Openssl dgst -sha1 -sign PrivateKey.pem -out Record1.sha1 Message.txt

我的代码如下:

import java.io.*;  
public class TestExec {  
    public static void main(String[] args) {  
        try {  
            File directory1 = new File("C:\\OpenSSL-Win32\\bin\\");
            System.out.println(directory1.toString());
            String[] commandArray = {"Openssl dgst -sha1 -sign PrivateKey.pem -out Record1.sha1 Message.txt"};
            Process p2 = Runtime.getRuntime().exec(commandArray, null, directory1);

    } catch (IOException e) {  
        e.printStackTrace();  
    }  
}  
} 

我收到以下 IOException:

java.io.IOException: Cannot run program "Openssl dgst -sha1 -sign PrivateKey.pem -out Record1.sha1 Message.txt" (in directory "C:\OpenSSL-Win32\bin"): CreateProcess error=2, The system cannot find the file specified
    at java.lang.ProcessBuilder.start(Unknown Source)
    at java.lang.Runtime.exec(Unknown Source)
    at package1.TestExec.main(TestExec.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>(Unknown Source)
    at java.lang.ProcessImpl.start(Unknown Source)
    ... 3 more

请帮我解决这个问题。谢谢。

4

1 回答 1

0

设置工作目录并不意味着 java 会在该目录中查找可执行文件。它只是设置为可执行文件的当前目录。

我不确定 Windows 上关于 PATH 环境变量的确切行为,但有它当前设置的...

无论如何,我会尝试使用它

String[] commandArray = {"C:\\OpenSSL-Win32\\bin\\Openssl\\Openssl dgst -sha1 -sign PrivateKey.pem -out Record1.sha1 Message.txt"};
于 2012-09-13T07:18:57.950 回答