Sorry, this answer brings nothing new but the code in the accepted answer is so terrible that I just have to post it. It's just a polished version, nothing more. So consider copy/pasting from here but upvoting the accepted answer rather than this one.
public static void addX509CertificateToTrustStore(String certPath, String certAlias, String storePath, String storePassword, String storeType)
throws FileNotFoundException, KeyStoreException, CertificateException, IOException, NoSuchAlgorithmException {
char[] storePasswordCharArr = Objects.requireNonNull(storePassword, "").toCharArray();
KeyStore keystore;
try (FileInputStream storeInputStream = new FileInputStream(storePath);
FileInputStream certInputStream = new FileInputStream(certPath)) {
keystore = KeyStore.getInstance(storeType);
keystore.load(storeInputStream, storePasswordCharArr);
CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
Certificate certificate = certificateFactory.generateCertificate(certInputStream);
keystore.setCertificateEntry(certAlias, certificate);
} finally {
}
try (FileOutputStream storeOutputStream = new FileOutputStream(storePath)) {
keystore.store(storeOutputStream, storePasswordCharArr);
} finally {
}
}