You could try a different approach again - since keytool
is written in Java and it is delivered with the JDK, you can actually instantiate the keytool class directly, like in this answer. This approach will let you generate a self-signed certificate in the JKS file of your choice, but it won't give you programmatic access to the generated certificate.
Just watch out, under Java 7 you will need to do new sun.security.tools.KeyTool()
, but under Java 8 the class has been moved and you will need to do new sun.security.tools.keytool.Main
. And of course it only works for the Oracle JDK, the APIs are internal and not guaranteed to be present in any future Java version, etc., etc.