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);
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.
See Question&Answers more detail:
os