ProcessBuilder并运行包含空格的OpenSSL命令 [英] ProcessBuilder and running OpenSSL command which contains spaces
问题描述
在Ubuntu environemnt中使用我的jar执行openSSL命令时遇到问题。
我已经得出结论,这是因为文件路径中的空间正在作为命令中的参数传递,例如SHA 256在下面的命令中。
我已经使用了两个进程和 ProcessBuilder
类来执行相同的操作:
I am facing a problem while executing openSSL command using my jar in Ubuntu environemnt.
I have concluded that this is happening because of the space in the path of the file which is being passed as a parameter in the command e.g. SHA 256 in below command.
I have used both process and ProcessBuilder
classes for executing the same:
首先 :
First:
String certFilePath = "/home/mplusuer/Desktop/Nishant/210515/TestData/TestData/SHA 256/nishant.cer"
String []cmdGetAlgorithm = new String[3];
cmdGetAlgorithm[0] = "openssl x509 -in";
cmdGetAlgorithm[1] = certFilePath;
cmdGetAlgorithm[2] = "-noout -text -certopt no_subject,no_header,no_version,no_serial,no_validity,no_subject,no_issuer,no_pubkey,no_sigdump,no_aux,no_extensions";
ProcessBuilder pb = new ProcessBuilder(cmdGetAlgorithm[0], cmdGetAlgorithm[1],cmdGetAlgorithm[2]);
// setup other options ...
Process processGetAlgorithm = pb.start();
processGetAlgorithm.waitFor();
第二 :
Second:
Runtime runtime = Runtime.getRuntime();
String cmdGetAlgorithm = "openssl x509 -in "
+ certFilePAth
+ " -noout -text -certopt no_subject,no_header,no_version,no_serial,no_validity,no_subject,no_issuer,no_pubkey,no_sigdump,no_aux,no_extensions ";
Process processGetAlgorithm = runtime.exec(cmdGetAlgorithm);
最终命令如下所示,如果在命令提示符下单独执行,则可以正常工作但在执行时使用失败java code:
the final command is as below, which works fine if executed separately on command prompt but fails when executed using java code:
openssl x509 -in /home/mplusuer/Desktop/Nishant/210515/TestData/TestData/SHA 256/suketu.cer \
-noout -text -certopt no_subject,no_header,no_version,no_serial,no_validity,no_subject, \
no_issuer,no_pubkey,no_sigdump,no_aux,no_extensions
我也使用以下方法来解决这个问题,但没有按预期工作:
I have used the below methods also for resolving this issue, but nothing worked as per expectation:
String quoted = "\"" + certFilePath + "\"";
String escaped = certFilePath.replace(" ", "\\ ");
请查看并帮助我解决问题。
Please see to it and help me in resolving the same.
推荐答案
cmdGetAlgorithm[0] = "openssl x509 -in";
...
正如@immibis在注释, arg [0]
是程序名称。所以向量应该类似于:
As @immibis stated in the comments, arg[0]
is the program name. So the vector should look something like:
cmdArg[0] = "/usr/local/ssl/bin/openssl";
cmdArg[1] = "x509";
cmdArg[2] = "-in";
cmdArg[3] = certFilePAth;
cmdArg[4] = "-noout"
cmdArg[5] = "-text";
cmdArg[6] = "-certopt";
cmdArg[7] = "no_subject,no_header,no_version,no_serial,no_validity," +
"no_issuer,no_pubkey,no_sigdump,no_aux,no_extensions ";
您应该始终指定可执行文件的完整文件名,以确保您运行的是预期的可执行文件,而不是对手种下的东西。
You should always specify the complete filename of the executable to ensure you are running the intended executable, and not something planted by an adversary.
这篇关于ProcessBuilder并运行包含空格的OpenSSL命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!