@SuppressWarnings("unchecked")
    public static void main(final String[] args) {
        final HashMap<String, String> argsMap = new HashMap<>();
        for (int i = 0; i < args.length; i++) {
            final String[] split = args[i].split("=");
            if (split.length > 1) {
                argsMap.put(split[0], split[1]);
            } else {
                argsMap.put(split[0], "");
            }
        }

        final String keyPass = argsMap.get("keypass");
        final String alias = argsMap.get("alias");
        final String keystore = argsMap.get("keystore");
        final String keyfile = argsMap.get("keyfile");
        final String certfile = argsMap.get("certfile");

        try {
            final KeyStore ks = KeyStore.getInstance("JKS", "SUN");
            ks.load(null, keyPass.toCharArray());

            try (final OutputStream outputStream = Files.newOutputStream(Paths.get(keystore))) {
                ks.store(outputStream, keyPass.toCharArray());
            }
            try (final InputStream inputStream = Files.newInputStream(Paths.get(keystore))) {
                ks.load(inputStream, keyPass.toCharArray());
            }

            final byte[] key = Files.readAllBytes(Paths.get(keyfile));
            final KeyFactory kf = KeyFactory.getInstance("RSA");

            final PKCS8EncodedKeySpec keysp = new PKCS8EncodedKeySpec(key);
            final PrivateKey ff = kf.generatePrivate(keysp);

            final CertificateFactory cf = CertificateFactory.getInstance("X.509");
            final byte[] bytes = Files.readAllBytes(Paths.get(certfile));

            final Collection<Certificate> c = (Collection<Certificate>) cf.generateCertificates(
                    new ByteArrayInputStream(bytes));
            Certificate[] certs = new Certificate[c.size()];

            if (c.size() == 1) {
                System.out.println("One certificate, no chain");
                final Certificate cert = cf.generateCertificate(new ByteArrayInputStream(bytes));
                certs[0] = cert;
            } else {
                System.out.println("Certificate chain length: " + c.size());
                certs = c.toArray(new Certificate[0]);
            }

            ks.setKeyEntry(alias, ff, keyPass.toCharArray(), certs);
            try (final OutputStream outputStream = Files.newOutputStream(Paths.get(keystore))) {
                ks.store(outputStream, keyPass.toCharArray());
            }
        } catch (final IOException
                | NoSuchAlgorithmException
                | CertificateException
                | KeyStoreException
                | NoSuchProviderException
                | InvalidKeySpecException e) {
            e.printStackTrace();
        }
    }
<p>
Class used to import keys from openssl into a Java key store.
</p>
<p>
E.g.
</p>
<pre>
# Server
export SERVER=example

# Gen Private Key
openssl genrsa -des3 -out $SERVER.key 1024

# Gen CSR
openssl req -new -key $SERVER.key -out $SERVER.csr

# Copy CSR then Create Cert (cat then paste contents from CA)
cat $SERVER.csr
vi $SERVER.crt

# Create DER format Keys
openssl pkcs8 -topk8 -nocrypt -in $SERVER.key -inform PEM -out $SERVER.key.der -outform DER
openssl x509 -in $SERVER.crt -inform PEM -out $SERVER.crt.der -outform DER

# Now Import the Key using this tool
java ImportKey keystore=$SERVER.jks keypass=$SERVER alias=$SERVER keyfile=$SERVER.key.der certfile=$SERVER.crt.der

# Also inport the CA if required
keytool -import -alias CA -file root_ca.crt -keystore $SERVER.jks -storepass $SERVER

# List contents at end
keytool -list -keystore $SERVER.jks -storepass $SERVER
</pre>
stroom-util/src/main/java/stroom/util/cert/ImportKey.java