I have JAVA_HOME=C:\Users\myuser\jdk1.8.0_65
JRE Sytem Library in Eclipse points to C:\Users\myuser\jdk1.8.0_65
java -version
gives C:\Users\myuser\jdk1.8.0_65
Importing certificate by:
C:\Users\myuser\jdk1.8.0_65\bin\keytool.exe -importcert -trustcacerts -keystore C:\Users\myuser\jdk1.8.0_65\jre\lib\security\cacerts -storepass changeit -noprompt -alias ldap -file C:\Users\myuser\certificates\ldap_cer.cer
Keytool list gives:
C:\Users\myuser>keytool -list
Enter keystore password:
Keystore type: JKS
Keystore provider: SUN
Your keystore contains 0 entries
When I try to check the certificate with below program I can see the entry, why keytool -list
doesn't give me the same result? How can I add certificate? What I am missing?
package com.;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.Certificate;
import java.util.Enumeration;
public class HandShake {
static FileInputStream is;
public static void main(String[] args) {
try {
File file = new File("C://Users//myuser//jdk1.8.0_65//jre//lib//security//cacerts");
System.setProperty("javax.net.ssl.keyStore", file.getAbsolutePath());
System.out.println(System.getProperty("javax.net.ssl.keyStore"
));
is= new FileInputStream(file);
KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
String password = "changeit";
keystore.load(is, password.toCharArray());
Enumeration enumeration = keystore.aliases();
while(enumeration.hasMoreElements()) {
String alias = (String)enumeration.nextElement();
if(alias.equals("ldap")){
System.out.println("alias name: " + alias);
Certificate certificate = keystore.getCertificate(alias);
System.out.println(certificate.toString());
}
}
} catch (java.security.cert.CertificateException e4) {
e4.printStackTrace();
} catch (NoSuchAlgorithmException e1) {
e1.printStackTrace();
} catch (FileNotFoundException e2) {
e2.printStackTrace();
} catch (KeyStoreException e3) {
e3.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(null != is)
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}