code: private void importKeyPair()
              throws IOException, GeneralSecurityException, Exception
      {
      // Load the private key
          PrivateKey privateKey = loadPrivateKey(privateKeyFile);
      
          // Import the cert...
      Certificate[] certChain = loadCertChain(certFile);
  
          // Load any existing KeyStore
          if (keyPassword == null)
              keyPassword = keyStorePassword;
  
          KeyStore keyStore = KeyStore.getInstance(keyStoreType);
          InputStream keyStoreStream = null;
          try
          {
          keyStoreStream = new FileInputStream(keyStoreFile);
          System.out.println("Will load " + keyStoreType
                     + " keystore: " + keyStoreFile);
          }
          catch (FileNotFoundException e)
          {
              // That's OK, we'll just create a new one
              System.out.println("Creating keystore: " + keyStoreFile);
          }
  
          // The load method can accept a null keyStoreStream.
          keyStore.load(keyStoreStream, keyStorePassword.toString().toCharArray());
  
          if (keyStoreStream != null)
          {
              keyStoreStream.close();
              System.out.println("Keystore loaded OK...");
          }
  
          // Insert the new key pair
          keyStore.setKeyEntry(alias,
                               privateKey,
                               keyPassword.toString().toCharArray(),
                               certChain);
  
          // Save the KeyStore
          FileOutputStream keyStoreOut = new FileOutputStream(keyStoreFile);
          keyStore.store(keyStoreOut,
                         keyStorePassword.toString().toCharArray());
          keyStoreOut.close();
  
          System.out.println("Keys have been written to keystore");
      }
docs: Import a key/cert pair into the keystore.
  <p> Class variables hold the state information required for this
  operation.
  @throws IOException if there are problems with file IO
  @throws GeneralSecurityException if there are cryptographic failures.
  @throws Exception on other exceptions, such as classloading failures.