Search code examples
javawindowsbatch-fileopensslprocessbuilder

Batch file execution with parameter - [Java - OpenSSL]


I am trying to generate certificate using openSSL commands in a .bat file and calling it from Java code -

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%

Subject passed from java code is not allowing me to execute bat file? I tried escaping forward slash but same error.

How to pass -subj[openSSL] via java code to bat file.

Note - this is working fine [Hard coded]

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"

Solution

  • Try this, 3 arguments to your process, the third needs to be a single command that will be passed to cmd.exe, and within that command you need to put quotes around the subject:

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