I am trying to use keytool to generate a certificate programmatically. In order to do that, I am first generating a keystore using the following command:
-genkeypair -alias alias -keyalg RSA -keysize 2048 -dname "CN=name,OU=ou,O=o,c=pt" -validity 365 -keystore teststore.jks -keypass testpass -storepass testpass -noprompt
On the command line this completes successfully, creating the file teststore.jks
without asking for the user's input. I need this since I will be using this command from a ProcessBuilder
instance.
I use the following code to generate the same command:
StringBuilder command = new StringBuilder();
command.append("keytool ");
command.append("-genkeypair");
command.append(" -keystore " + username + "store.jks");
command.append(" -alias " + username);
command.append(" -keyalg RSA");
command.append(" -keysize 2048");
command.append(" -dname \"CN="+username+", OU=FCT, O=UNL, L=Unknown, ST=Unknown, C=PT\"");
command.append(" -validity " + 365);
command.append(" -keypass " + certpassword);
command.append(" -storepass " + certpassword);
command.append(" -noprompt");
ProcessBuilder pb = new ProcessBuilder(command.toString());
pb.inheritIO();
pb.start();
When I run the program, I get the following output:
java.io.IOException: Cannot run program "keytool -genkeypair -keystore teststore.jks -alias alias -keyalg RSA -keysize 2048 -dname "CN=name, OU=ou, O=o, C=pt" -validity 365 -keypass ssc1415 -storepass ssc1415 -noprompt": error=2, No such file or directory
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
at UserRegistry.main(UserRegistry.java:29)
Caused by: java.io.IOException: error=2, No such file or directory
at java.lang.UNIXProcess.forkAndExec(Native Method)
at java.lang.UNIXProcess.<init>(UNIXProcess.java:185)
at java.lang.ProcessImpl.start(ProcessImpl.java:134)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)
... 1 more
Since I use pb.inheritIO()
I commented out the -dname
and the -noprompt
parts and it still terminated with the same error, so I ran out of ideas.
I succeeded in working around this issue by supplying a
String[]
and using thegetRuntime().exec()
method.I used:
This enabled me to create a key with dynamic arguments passed in to a program, which in my project made it easier to build multiple certificates at once.