Java KeyStore
PEM import
security
Java programming
digital certificates

Import a PEM certificate and key into a Java KeyStore (JKS)

Codemia Knowledge Hub ·

Java's keytool does not import PEM files directly, so convert the PEM certificate and private key to a PKCS12 file with OpenSSL first, then import that PKCS12 file into a JKS with keytool -importkeystore.

PEM vs JKS

A PEM file is a Base64 encoded DER certificate wrapped in header and footer lines:

 
-----BEGIN CERTIFICATE-----
(Base64 Encoded Certificate)
-----END CERTIFICATE-----

PEM files can hold certificates, certificate chains or private keys, and are common with Apache servers and OpenSSL.

A Java KeyStore (JKS) is a binary, encrypted file that holds certificates and keys. It was the default keystore type for Keytool until Java 9, which switched the default to PKCS12.

What you need

  • Java Keytool, the key and certificate management utility.
  • OpenSSL, for the PEM to PKCS12 conversion.
  • The private key and certificate chain in PEM form.

Converting PEM to JKS

1. Convert PEM to PKCS12

bash
openssl pkcs12 -export -in cert.pem -inkey key.pem -out keystore.p12 -name myalias -certfile ca.pem -caname root
  • cert.pem: the certificate file.
  • key.pem: the private key file.
  • keystore.p12: the output PKCS12 file.
  • myalias: the alias for the entries.
  • ca.pem: the CA certificate file, if you have a chain. -certfile adds it to the PKCS12 file (-CAfile alone does not, it only takes effect with -chain).
  • root: the root CA name.

2. Import the PKCS12 file into JKS

bash
keytool -importkeystore -deststorepass changeit -destkeypass changeit -destkeystore keystore.jks -deststoretype JKS -srckeystore keystore.p12 -srcstoretype PKCS12 -srcstorepass somepassword -alias myalias
  • keystore.jks: the destination JKS file. -deststoretype JKS is needed on Java 9 and later, where keytool otherwise writes PKCS12 even to a .jks file.
  • changeit: the default keystore password (replace it with a strong one).
  • somepassword: the password you set when creating the PKCS12 file.

3. Verify the JKS contents

bash
keytool -list -v -keystore keystore.jks

Check that your alias and certificates appear in the output.

Before you change a keystore

  • Passwords: use strong passwords for both the PKCS12 and JKS files.
  • Compatibility: some Java tools do not fully support PKCS12 files directly, so converting to JKS avoids that.
  • Backups: back up the current keystore before changing it, especially in production.

Errors during the import

  • Inconsistent aliases: the alias you pass to keytool must match the one set during PKCS12 creation.
  • Password mismatches: wrong passwords cause errors when accessing keys. Double-check them at each step.
  • SSLHandshakeException: usually means the CA certificates were not imported correctly. Check and re-import them.
StepCommand/Tool UsedNotes
Convert PEM to PKCS12openssl pkcs12 -exportExport certificates and keys with OpenSSL.
Import into JKSkeytool -importkeystoreUse Keytool to convert PKCS12 to JKS.
Verify JKSkeytool -listCheck JKS contents to ensure correctness.
Alias ConsistencyEnsure matching aliasesConsistency between all filenames and aliases is critical.
Password HandlingStrong, consistent passwordsUse secure passwords for all files.
BackupsManual (before changes)Always maintain backups of your keystores.

Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design