Search code examples
javacertificatekeytool

Passing parameters to Keytool from java swing using cmd


I am trying to create a simple user interface for keytool to ease up the certificate generation process. I am aware that there are better ways to do this and creating a java swing interface is not the optimal solution.

I am getting the necessary parameters from swing components and pass it to the keytool as follow:

try {
    Process p = Runtime
                    .getRuntime()
                    .exec("cmd /c start cmd.exe /K \"keytool.exe &"+command.toString()+" end\"");
} catch (IOException e) {
    e.printStackTrace();
}

Now the problem is after passing the parameters nothing is happening and the cmd shows me the available options in keytool only.(same as entering keytool in cmd)

Here you can find the value of command String:

    command.append("keytool -genkey");//hardcoded for now, I'm using this method only
    command.append(" ");
    command.append("-keyalg");
    command.append(" ");
    command.append(algorithm.getSelectedItem()); //comboBox, value RSA/DSA/...
    command.append(" ");
    command.append("-alias");
    command.append(" ");
    command.append(alias.getText());//textfield value signedKey
    command.append(" ");
    command.append("-keystore");
    command.append(" ");
    command.append("selfsigned.jks");//hardcoded for now
    command.append(" ");
    command.append("-validity");
    command.append(" ");
    command.append(validity.getText());//textfield, value 365
    command.append(" ");
    command.append("-keysize");
    command.append(" ");
    command.append(keySize.getText());//depends on selected algorithm, values 2048/1024/...

I am new to keytool and for some reason I really cannot figure out what is wrong here, is it my approach or is it the parameters that I am passing...

P.S: sample of command String: keytool -genkey -keyalg RSA -alias signedKey -keystore selfsigned.jks -validity 365 -keysize 2048


Solution

  • The problem was in the String I am using to pass parameter cmd /c start cmd.exe /K "keytool.exe &-genkey -keyalg RSA -alias signedKey -keystore selfsigned.jks -validity 365 -keysize 2048 end"

    The end makes the problem and once it s been removed it's all good.

    Special thanks to Jim Garrison