Sample code for 30+ languages & platforms
Java

Load Certificate from .cer and Private Key from .pem

See more Certificates Examples

Load a certificate from a .cer and its associated private key from a .pem.

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.Cert;
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.StringBuilder;

public class ChilkatExample {

    public static void main(String[] args) {
        Cert cert = new Cert();
        try {
            cert.loadFromFile("C:/certs_and_keys/Certificate.cer");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        StringBuilder sbPem = new StringBuilder();
        try {
            sbPem.loadFile("C:/certs_and_keys/PrivateKey.pem", "utf-8");
        } catch (ChilkatException e) {
            System.out.println("Failed to load private key PEM");
            return;
        }

        try {
            cert.setPrivateKeyPem(sbPem.getAsString());
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        System.out.println("The certificate and associated private key are now loaded and ready for signing.");
    }
}