Getting a certificate from a Java keystore

I assumed here that you created the alias in the default keystore described in the question What is a keystore?.

Programmatically:

Main.java:

import java.security.cert.Certificate;
import java.security.*;
import java.io.*;
 
public class Main {
   public static void main(String []args) {
      try {
         KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
     
         InputStream in = new FileInputStream(System.getProperty("user.home") + "\" + 
                                              ".keystore");
 
         keyStore.load(in, "esuspass".toCharArray());
     
         Certificate cert = keyStore.getCertificate("mykey");
 
         System.out.println(cert);
      }  
      catch (KeyStoreException e) { 
         System.out.println(e);
      } 
      catch (java.security.cert.CertificateException e) {
         System.out.println(e);
      } 
      catch (NoSuchAlgorithmException e) {
         System.out.println(e);
      } 
      catch (java.io.IOException e) {
         System.out.println(e);
      } 
   }
}

outputs:

C:> java Main
[
[
  Version: V1
  Subject: CN=Joris Van den Bogaert, OU=ESUS Team, O="Esus, Inc", L=Meerbeek, ST
=Unknown, C=BE
  Signature Algorithm: MD5withRSA, OID = 1.2.840.113549.1.1.4

  Key:  RSA Public Key
            modulus: 10001
    public exponent: d395abbc7bd76b47c60a5e6e30cb7b74d8878706fe757d063daf737d8cd
3bf70447eaefd7ebaaab4d267898d8cd0680983a69b2e78c1d14ec27bfcff04da068e2dded11e323
4ecb900955295f96036ed0ddb919c5036bdb422415af60d81b793dbb40709f3fdd01609c470a52c5
e9ddc6879649122df5591d6f95efdbdf95889

  Validity: [From: Mon Aug 06 13:29:25 CEST 2001,
               To: Sun Nov 04 12:29:25 CET 2001]
  Issuer: CN=Joris Van den Bogaert, OU=ESUS Team, O="Esus, Inc", L=Meerbeek, ST=
Unknown, C=BE
  SerialNumber: [    3b6e7f95 ]

]
  Algorithm: [MD5withRSA]
  Signature:
0000: 94 19 99 85 71 95 6B C6   20 A0 42 C9 0E D1 4C D0  ....q.k. .B...L.
0010: 7A D6 30 38 4A C7 9E 65   72 F0 63 85 3D 46 74 48  z.08J..er.c.=FtH
0020: 7D 1D 70 A4 0A C9 A2 FC   CB 01 04 FA C5 31 39 1A  ..p..........19.
0030: 9C DC EA 07 BD 2C AD 7C   B0 D3 8E 00 16 90 E4 0E  .....,..........
0040: DB 73 3A AC 15 9F 6D 3F   7E 61 E6 E1 10 E5 A7 15  .s:...m?.a......
0050: 1C F5 B2 88 60 4C 55 3D   6B 38 22 B8 12 D6 B3 AF  ....`LU=k8".....
0060: 3A 81 61 C1 8C B3 CC 33   18 20 CA C8 B7 18 15 88  :.a....3. ......
0070: 67 6A C4 F7 D4 95 7E 80   7F DC F6 C6 79 36 86 C2  gj..........y6..

]

Using keytool:

C:> c:jdk1.3binkeytool -export -alias mykey -file mykey.cer
Enter keystore password:  esuspass
Certificate stored in file <mykey.cer>