Reputation: 1
I am trying to run keytool commands from ProcesBuilder of JAVA but it is failing with below error:
"Illegal option: -genkeypairKey and Certificate Management ToolCommands: -certreq Generates a certificate request -changealias
Changes an entry's alias -delete Deletes an entry -exportcert Exports certificate -genkeypair
Generates a key pair -genseckey Generates a secret key -gencert Generates certificate from a certificate request -importcert
Imports a certificate or a certificate chain -importpass Imports a password -importkeystore Imports one or all entries from another keystore
-keypasswd Changes the key password of an entry -list Lists entries in a keystore -printcert
Prints the content of a certificate -printcertreq Prints the content of a certificate request -printcrl
Prints the content of a CRL file -storepasswd Changes the store password of a keystoreUse
"keytool -command_name -help" for usage of command_name"
Below is the Code snippet for the same:
public void testName() throws Exception {
{
String[] commands = { "keytool ", " -genkeypair ", " -keyalg ", " RSA ", " -keysize ", " 2048 ",
" -alias ", " rootkey ", " -dname ", "DNAME_INFORMATION", " -keystore ", " .keystore ",
" -storetype ", " PKCS12 ", " -storepass ", " pass ", " -keypass ", " pass" };
ProcessBuilder probuilder = new ProcessBuilder(commands);
// You can set up your work directory
probuilder.directory(new File(System.getenv("JAVA_HOME") + "\\bin"));
Process process = probuilder.start();
// Read out dir output
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
final StringBuilder commandStr = new StringBuilder();
for (final String command : commands) {
commandStr.append(command);
commandStr.append(" ");
}
System.out.printf("Output of running %s is:\n", commandStr.toString());
is = process.getErrorStream();
if (null != is) {
isr = new InputStreamReader(is, Charsets.UTF_8);
br = new BufferedReader(isr);
}
StringBuffer sbuffer = new StringBuffer();
if (null != br) {
while ((line = br.readLine()) != null) {
sbuffer.append(line);
}
}
// If sbuffer contains some value then error has occured.
// Wait to get exit value
try {
int exitValue = process.waitFor();
System.out.println("\n\nExit Value is " + exitValue);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
If we run the same command from %JAVA_HOME%/bin directory (i.e. cmd) then it creates the .keystore file with out any error.
Command: keytool -genkeypair -keyalg RSA -keysize 2048 -alias rootkey -dname "DNAME_INFORMATION" -keystore .keystore -storetype PKCS12 -storepass pass -keypass pass
Please advise if anyone have faced any such issue.
Thanks.
Upvotes: 0
Views: 880
Reputation: 2566
You will have to remove the spaces around your arguments.
String[] commands = { "keytool ", "-genkeypair", "-keyalg", "RSA", "-keysize", "2048",
"-alias", "rootkey", "-dname", "DNAME_INFORMATION", "-keystore", ".keystore",
"-storetype", "PKCS12", "-storepass", "pass", "-keypass", "pass" };
Btw, you cann access a key store directly, see the KeyStore class.
Upvotes: 0