Java get default keystore
May 02, 2011 15:59:37 Last update: February 25, 2012 09:16:37
This code snippet gets the default keystore used by the Java
The default keystore used by the above code is:
keytool and displays the list of aliases along with the key type (certificate or private key).
import java.io.File; import java.io.FileInputStream; import java.security.*; import java.util.Enumeration; public class GetDefaultKeyStore { public static void main(String[] args) throws Exception { KeyStore ks = getDefaultKeyStore(); Enumeration<String> aliases = ks.aliases(); while(aliases.hasMoreElements()) { String alias = aliases.nextElement(); System.out.print(alias); System.out.println(ks.isCertificateEntry(alias) ? ": certificate" : ": private key"); } } private static KeyStore getDefaultKeyStore() throws Exception { KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType()); ks.load(new FileInputStream( new File(System.getProperty("user.home"), ".keystore") ), null ); return ks; } }
The default keystore used by the above code is:
$HOME/.keystore.