How to Fix SunCertPathBuilderException: Unable to Find Valid Certification Path to Requested Target Error in Java

Muhammad Zeeshan Feb 02, 2024
  1. What Is the sun.security.provider.certpath.SunCertPathBuilderException in Java
  2. Causes of sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
  3. Solution of sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
How to Fix SunCertPathBuilderException: Unable to Find Valid Certification Path to Requested Target Error in Java

Today’s post will discuss potential reasons for unable to find valid certification path to requested target and its solution in Java.

But, before we continue, you must clearly understand SSL encryption and Certificates since a clear understanding of SSL Certificates is required.

SSL encryption and Certificates: an HTTPS connection cannot function properly without an SSL certificate. Through the use of a set of public and private keys, it substantiates the claimed identity of the service or website.

The private key encrypts or signs everything that is delivered by the domain, and the client apps validate the identification by utilizing the public keys.

What Is the sun.security.provider.certpath.SunCertPathBuilderException in Java

The sun.security.provider.certpath.SunCertPathBuilderException is a class for handling exceptions in Java that checks the server’s SSL certificates that the Java program is attempting to connect to. This class was named after the provider that created the certificates.

The following is the error message shown to the developer when they encounter this issue.

Caused by : sun.security.provider.certpath.SunCertPathBuilderException
    : unable to find valid certification path to requested target

It can be thrown during any action involving visiting an encrypted service using SSL.

SSL-protected APIs and websites are checked for trustworthiness by the Java framework before they can be accessed. This is accomplished by examining its internal trusted list to see whether the root certificate authority signed is in there.

Java throws this error if it can’t discover any reference to the service. Using a self-signed certificate to access a service signed by someone else’s digital signature will always fail.

Causes of sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

The only way Java will be able to connect to another program via SSL is if it can trust HTTPS, etc. There is a truststore usually $JAVA HOME/lib/security/cacerts in the Java world where you store your trust certificates.

This exception is triggered by a self-signed certificate or a certificate chain that does not exist in the Java trust store. Java refuses to connect to the application because it does not trust the certificate.

Solution of sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

  1. To begin, determine whether the issue stems from the certificate or the network. To verify this, open the URL in Google Chrome or your preferred browser and see whether it works.

    In this case, an invalid certificate setting on the application client must be the source of the exception. There may be a firewall or network settings issue if the browser cannot access it.

  2. Get the server’s SSL Certificate after determining if there’s an issue with the certificate settings. This might be the root certificate in the simplest of circumstances, but in most cases, a certificate chain and intermediate certificate authority are required.

    You can check SSL Certificate validity by following the below steps.

  • Open your browser and click on the lock in the left upper corner.

    Chrome lock of URL

  • Then click on Connection is secure.

    Connection is secure

  • At last, you will get the site’s status if it’s valid.

    Certificate is valid

  1. If you want certificate details exported, follow the below steps.
  • Select the lock symbol next to the URL bar, then click the Connection is secure > Certificate is valid.

    Certificate is valid

    Certificate

  • Click on the Details section and export a certificate by clicking on the Copy to File button.

    click details

    Copy to File

    export wizard

  • Export DER encoded binaries.

    export der

  • Choose the directory where you want to save and name the file.

    Browse

  • When you click Finish, you’ll see a message The export was successful.

    The export was successful

  1. Start a trust store entry with a new certificate. The key tool utility can be used to do this.

    Execute the following command to create a new trust store.

    keytool - import - file C :\cascerts\firstCA.cert - alias firstCA - Keystore myTrustStore
    
  2. Restart your program to complete the process.

Muhammad Zeeshan avatar Muhammad Zeeshan avatar

I have been working as a Flutter app developer for a year now. Firebase and SQLite have been crucial in the development of my android apps. I have experience with C#, Windows Form Based C#, C, Java, PHP on WampServer, and HTML/CSS on MYSQL, and I have authored articles on their theory and issue solving. I'm a senior in an undergraduate program for a bachelor's degree in Information Technology.

LinkedIn

Related Article - Java SSL

Related Article - Java Error