Search code examples
javaexceptionx509certificatepki

How to open X509 certificate in java?


I was trying to open a crt certificate in java and hence thereby get some parameters from the crt. I used the following code:

inStream = new FileInputStream("sbi.crt");
CertificateFactory cf = CertificateFactory.getInstance("X.509");
X509Certificate cert = (X509Certificate)cf.generateCertificate(inStream);

When I run the code, the compiler throws certificate exception at line 2. Is it because of the certificate that I'm using? Or what changes in code should I make?

Or is there any alternative way to access a certificate and extract the details?


Solution

  • When I run the code, the compiler throws certificate exception at line 2.

    No it doesn't. The compiler doesn't throw exceptions. It prints compilation errors.

    Is it because of the certificate that I'm using?

    No.

    Or what changes in code should I make?

    Catch the exception named in the error message, or declare it to be thrown by the method.