Avatar ·

PKIX path building failed error during HTTPS connection in Java — how to fix?

Hello everyone! I'm writing a small Java application that should send a GET request to an external REST API over HTTPS. I'm using the standard HttpURLConnection. The code is simple, but when executed, an exception is thrown: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target Here is my code: URL url = new URL("https://example.com/api/data"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); int responseCode = conn.getResponseCode(); // error here I've already checked: the URL is correct, the site is accessible from the browser. But Java cannot establish a trusted connection. I'm a beginner in Java and don't understand how to solve this problem. I googled it — people write about keystore, truststore, but it's too complicated. Maybe there is a simple way? Or do I need to deal with certificates? I would be grateful for your help.