How to obtain the location of cacerts of the default java installation?

前端 未结 6 659
情书的邮戳
情书的邮戳 2020-12-02 09:56

I am looking on how how to obtain the location of cacerts of the default java installation, when you do not have JAVA_HOME or JRE_HOME

6条回答
  •  春和景丽
    2020-12-02 10:22

    If you need to access those certs programmatically it is best to not use the file at all, but access it via the trust manager. The following code is from a OpenJDK Test case (which makes sure the built cacerts collection is not empty):

    TrustManagerFactory trustManagerFactory =
        TrustManagerFactory.getInstance("PKIX");
    trustManagerFactory.init((KeyStore) null);
    TrustManager[] trustManagers =
        trustManagerFactory.getTrustManagers();
    X509TrustManager trustManager =
        (X509TrustManager) trustManagers[0];
    X509Certificate[] acceptedIssuers =
        trustManager.getAcceptedIssuers();
    

    So you don’t have to deal with file location or keystore password.

提交回复
热议问题