0

I am using below lines of code to verify broken links

   huc = (HttpURLConnection) (new URL(url).openConnection());
   huc.setRequestMethod("HEAD");
   huc.setReadTimeout(5000);
   huc.connect();
   respCode = huc.getResponseCode();

when script enters huc.connect() line , it throws below exception.

javax.net.ssl.SSLHandshakeException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

How can I solve it? I need to run this script locally and also in github actions pipeline.

James Z
  • 12,209
  • 10
  • 24
  • 44
Giri
  • 9
  • 1

1 Answers1

2

You get this error when your Website certificate is not added to Trusted store. Please follow below step to add certificate to trusted store.

  1. Open the website and Download the certificate by clicking on the lock icon->Connection is secure->Certification-> click on the root->Certification path->Copy to file-> and store in your local system with an alias name.

cert1 cert2 cert3

  1. Now you need to add certificate to $JAVA_HOME\lib\security\cacerts using below command
keytool -import -noprompt -trustcacerts -alias <AliasName> -file   <certificate> -keystore <KeystoreFile> -storepass <Password>

eg:

keytool -import -noprompt -trustcacerts -alias myaddedcertaliasname
 -file /localsystempath/to/my/cert/myCert.cer -keystore /path/to/my/jdk/jre/lib/security/cacerts/keystore.jks -storepass
 changeit

Please note the default password for key store is changeit.

Once you import the certificate to your key store you should no more get this error

Sonali Das
  • 943
  • 1
  • 7
  • 24