I am trying to add a private key pair to an existing Java KeyStore file. When I execute the following command via the Terminal, it works. (ie. when I execute "keytool -list -v -keystore ecekeystore.jks", I can see the newly added key)
keytool -genkey -alias blabla -keyalg RSA -keystore ecekeystore.jks -dname "CN=MyName, OU=blabla, O=blabla, L=blabla, S=blabla, C=US" -storepass password1 -keypass password2
But when I run the following piece of Java code, nothing changes in my keystore file.
try { Runtime rt = Runtime.getRuntime(); String command = "keytool " + "-genkey -alias blabla -keyalg RSA " + "-keystore ecekeystore.jks " + "-dname \"CN=MyName, OU=blabla, O=blabla, L=blabla, S=blabla, C=US\" " + "-storepass password1 " + "-keypass password2"; System.out.println(command); Process pr = rt.exec(command); return true; } catch (IOException e) { e.printStackTrace(); } I'd appreciate any help!
SOLVED:
Runtime rt = Runtime.getRuntime(); try { String[] cmdArray = new String[14]; cmdArray[0] = "keytool"; cmdArray[1] = "-genkey"; cmdArray[2] = "-alias"; cmdArray[3] = "blabla"; cmdArray[4] = "-keyalg"; cmdArray[5] = "RSA"; cmdArray[6] = "-keystore"; cmdArray[7] = "ecekeystore.jks"; cmdArray[8] = "-dname"; cmdArray[9] = "CN=MyName, OU=blabla, O=blabla, L=blabla, S=blabla, C=US"; cmdArray[10] = "-storepass"; cmdArray[11] = "password1"; cmdArray[12] = "-keypass"; cmdArray[13] = "password1"; Process pr = rt.exec(cmdArray); InputStream is = pr.getInputStream(); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); String line; System.out.printf("Output is:\n"); while ((line = br.readLine()) != null) { System.out.println(line); } return true; } catch (IOException e) { e.printStackTrace(); }
Process.execrequires arguments to be specified in a separate array parameter.Runtime.execpreviously.-keystoreis a relative path, so if you're not in the appropriate directory...