Search code examples
javakeytool

certs are imported. But can not list them


In my new windows 10 laptop I successfully imported some certs:

 C:\windows\system32>keytool -import -trustcacerts -storepass changeit -noprompt -alias <cert name> -file C:/ca-certs/<cert name>.crt -cacerts -v
Certificate was added to keystore
[Storing C:\Program Files\Zulu\zulu-17\lib\security\cacerts]

Then I tried to list them. But I am getting this exception:

    C:\windows\system32>keytool -list
keytool error: java.lang.Exception: Keystore file does not exist: C:\Users\tester\.keystore

-import command imported the certs to the cacerts file. But the -list command tries to find it from .keystore file. Did I miss any configuration. I remember I did the same thing in my older laptop but I did not have issues.


Solution

  • The "keytool" program defaults to a keystore called ".keystore" in your home directory, not the keystore you just created.

    By default the keytool will generate a keystore in the user’s home directory (Linux/macOS: $HOME/.keystore, Windows: $env:USERPROFILE/.keystore). Use the -keystore parameter to specify a custom path.

    Add the "-keystore" option to specify the location of the keystore file you created.

    C:\windows\system32> keytool -list -keystore "C:/ca-certs/<cert name>.crt"
    Enter keystore password: <YourPasswordHere>
    

    That will list everything, but you can filter it by your alias if needed:

    C:\windows\system32> keytool -list -keystore "C:/ca-certs/<cert name>.crt" -alias "<cert name>"
    Enter keystore password: <YourPasswordHere>
    

    More examples of keytool commands are at The Most Common Java Keytool Keystore Commands.