使用参数执行批处理文件 - [Java - OpenSSL]

时间:2017-08-08 18:52:20

标签: java windows batch-file openssl processbuilder

我正在尝试使用.bat文件中的openSSL命令生成证书并从Java代码调用它 -

String loc = "C:\\Users\\xyz\\Desktop\\myFolder\\bin";
String subject = "/C=GB/ST=London/L=London/O=Global Security/OU=software/CN=blah/emailAddress=a.b@xyz.com";
List<String> cmds = Arrays.asList("cmd.exe", "/C", "start C:/Users/xyz/Desktop/myBat.bat", loc, subject);
ProcessBuilder builder = new ProcessBuilder(cmds);           
Process proc = builder.start();

myBat.bat

set root=%1
cd /D %root%
set arg2=%2
openssl req -new -out server.csr -key server.key -config openssl.cnf -subj %arg2%

从java代码传递的主题不允许我执行bat文件? 我试图逃避正斜杠但同样的错误。

如何通过java代码将-subj [openSSL]传递给bat文件。

注意 - 这工作正常[硬编码]

openssl req -new -out server.csr -key server.key -config openssl.cnf -subj "/C=GB/ST=London/L=London/O=Global Security/OU=software/CN=blah/emailAddress=a.b@xyz.com"

1 个答案:

答案 0 :(得分:1)

试试这个,3个参数进入你的进程,第三个参数需要是一个传递给cmd.exe的命令,并且在该命令中你需要在主题周围加上引号:

List<String> cmds = Arrays.asList(
        "cmd.exe",
        "/C",
        "start C:/Users/xyz/Desktop/myBat.bat " + loc + " \"" + subject + "\""
);