Import a Certificate into the Default Java Keystore

Solution from Stack Overflow

 From http://stackoverflow.com/questions/21076179/pkix-path-building-failed-and-unable-to-find-valid-certification-path-to-requ :

Only solution worked for me is

I wanted to import certificate for smtp.gmail.com

Enter command to view this certificate
D:\openssl\bin\openssl.exe s_client -connect smtp.gmail.com:465

2.Copy the Copy and save the lines between "-----BEGIN CERTIFICATE-----" and "-----END CERTIFICATE-----" into a 

3. keytool -import -alias smtp.gmail.com -keystore "%JAVA_HOME%/jre/lib/security/cacerts" -file C:\Users\Admin\Desktop\gmail.cer

4. Enter password changeit

5. Click yes to import the certificate

6. Restart java

now run the command and you are good to go

Known Issues

On Linux, you may need to run the keytool with sudo.