You call an HTTPS URL from Java and get this:
javax.net.ssl.SSLHandshakeException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target This post explains what it means and how to fix it properly. I reproduced it on a local test server so every command and output below is real (JDK 21, OpenSSL 3.5, Windows 11). Hostnames and paths on your system will differ.
What the error means
When Java opens a TLS connection, the server sends its certificate chain. Java then tries to build a path from that chain to a certificate it trusts. The trusted certificates live in a truststore. If no path ends at something in the truststore, you get unable to find valid certification path to requested target.
So the message is not about the server being down or the certificate being expired. It says the CA that signed this certificate is not in the truststore your JVM is using. The usual causes:
- A corporate TLS-inspection proxy. The proxy re-signs traffic with a company CA. Browsers trust it because IT installed it in the OS store. Java uses its own store and does not.
- A self-signed or internal CA. Test servers, staging environments, internal services.
- A missing intermediate certificate on the server. The server sends only its own certificate, not the CA in between.
The lab setup
To get a real error without touching a real site, I made a tiny CA and a server certificate for localhost, and served HTTPS from Python on 127.0.0.1:8443.
# Git Bash openssl req -x509 -newkey rsa:2048 -nodes -keyout ca.key -out ca.crt -days 30 \ -subj "/CN=Lab Root CA" \ -addext "basicConstraints=critical,CA:TRUE" -addext "keyUsage=critical,keyCertSign,cRLSign" openssl req -newkey rsa:2048 -nodes -keyout server.key -out server.csr -subj "/CN=localhost" printf "subjectAltName=DNS:localhost,IP:127.0.0.1\nbasicConstraints=CA:FALSE\n" > server.ext openssl x509 -req -in server.csr -CA ca.crt -CAkey ca.key -CAcreateserial \ -out server.crt -days 30 -extfile server.ext server.py:
import http.server, ssl, sys cert, key, port = sys.argv[1], sys.argv[2], int(sys.argv[3]) ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER) ctx.load_cert_chain(cert, key) srv = http.server.HTTPServer(("127.0.0.1", port), http.server.SimpleHTTPRequestHandler) srv.socket = ctx.wrap_socket(srv.socket, server_side=True) srv.serve_forever() Start it with python server.py server.crt server.key 8443.
The Java client, Client.java, is a single file that prints the exception chain:
import java.net.URI; import java.net.http.*; public class Client { public static void main(String[] args) throws Exception { HttpClient c = HttpClient.newHttpClient(); HttpRequest r = HttpRequest.newBuilder(URI.create(args[0])).build(); try { HttpResponse<String> resp = c.send(r, HttpResponse.BodyHandlers.ofString()); System.out.println("HTTP " + resp.statusCode()); } catch (Exception e) { Throwable t = e; System.out.println(e); while (t.getCause() != null) { t = t.getCause(); System.out.println("caused by: " + t); } } } } Run it:
$ java Client.java https://localhost:8443/ javax.net.ssl.SSLHandshakeException: (certificate_unknown) PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target caused by: sun.security.validator.ValidatorException: PKIX path building failed: ... caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target That is the error.
Step 1: See what the server sends
openssl s_client -connect localhost:8443 -showcerts </dev/null The full output is long. The lines that matter:
Certificate chain 0 s:CN=localhost i:CN=Lab Root CA -----BEGIN CERTIFICATE----- ... Verify return code: 21 (unable to verify the first certificate) s: is the subject, i: is the issuer. Here the server certificate was issued by Lab Root CA. On a real site behind an inspection proxy, you would see your company's CA as the issuer instead of the public CA you expect. That alone tells you the cause.
Each -----BEGIN CERTIFICATE----- block is a certificate you can copy into a .pem file. Pass the CA's certificate to -CAfile to check that it is the right one:
$ echo | openssl s_client -connect localhost:8443 -CAfile ca.crt ... Verify return code: 0 (ok) I ran the same s_client command from WSL Ubuntu against the Windows-hosted server on 127.0.0.1 and it showed the same chain.
Do not install a certificate just because it makes the error go away. Make sure you know who issued it and that you trust that issuer. For a corporate proxy, ask IT for the root certificate.
Step 2: Find which truststore Java uses
By default Java uses cacerts in the JDK, at $JAVA_HOME/lib/security/cacerts on current JDKs (older layouts differ; the debug flag below prints the exact path for yours). The default password is changeit. You can override the store with -Djavax.net.ssl.trustStore=<path> and -Djavax.net.ssl.trustStorePassword=<password>.
To check which one a run actually used, turn on trust manager debugging:
$ java -Djavax.net.debug=ssl:trustmanager Client.java https://localhost:8443/ ... TrustStoreManager.java:162|Inaccessible trust store: C:\Program Files\Eclipse Adoptium\jdk-21.0.11.10-hotspot\lib\security\jssecacerts ... TrustStoreManager.java:113|trustStore is: C:\Program Files\Eclipse Adoptium\jdk-21.0.11.10-hotspot\lib\security\cacerts trustStore type is: pkcs12 Java looks for jssecacerts first and falls back to cacerts, which is what those two lines show. Your application server, Gradle daemon or IDE may run a different JDK than the one on your PATH, so check the JDK that actually runs the failing code.
Step 3: Import the CA into a copy of cacerts
Do not edit the JDK's cacerts in place. JDK updates replace it, it often needs admin rights, and every Java program on the machine would trust your change. Copy it and edit the copy.
# Git Bash. Adjust the JDK path for your machine. cp "/c/Program Files/Eclipse Adoptium/jdk-21.0.11.10-hotspot/lib/security/cacerts" my-cacerts keytool -importcert -alias lab-root-ca -file ca.crt -keystore my-cacerts -storepass changeit -noprompt Certificate was added to keystore Check the entry:
$ keytool -list -keystore my-cacerts -storepass changeit -alias lab-root-ca lab-root-ca, Nov 16, 2026, trustedCertEntry, Certificate fingerprint (SHA-256): 91:4A:28:2F:98:E9:1A:69:91:F9:24:97:87:DF:71:DD:AF:8A:8C:3D:6E:B3:99:11:66:24:ED:94:0C:0D:D9:73 Without -noprompt, keytool shows the certificate and asks Trust this certificate? [no]:. Read it before answering yes.
Now point the program at the copy:
$ java -Djavax.net.ssl.trustStore=my-cacerts -Djavax.net.ssl.trustStorePassword=changeit Client.java https://localhost:8443/ HTTP 200 It works. The same command also worked from PowerShell. Because the copy still contains all the original public CAs, other HTTPS calls from that program keep working. If you set trustStore to a file that holds only your CA, every other HTTPS call will fail with the same PKIX error.
Import the CA certificate (the issuer), not just the server's own certificate. A server certificate is replaced every few months, and the CA certificate lasts years.
If you can't change the command line, the JAVA_TOOL_OPTIONS environment variable passes the same flags to any JVM started from that shell:
$ JAVA_TOOL_OPTIONS="-Djavax.net.ssl.trustStore=my-cacerts -Djavax.net.ssl.trustStorePassword=changeit" java Client.java https://localhost:8443/ Picked up JAVA_TOOL_OPTIONS: -Djavax.net.ssl.trustStore=my-cacerts -Djavax.net.ssl.trustStorePassword=changeit HTTP 200 The Picked up JAVA_TOOL_OPTIONS line is printed to stderr every time; it is a notice, not an error (PowerShell shows it in red). App servers, build tools and IDE run configurations each have their own JVM-options setting for the same two flags.
Why not disable verification?
Many answers on the web give you a TrustManager that accepts everything, or a HostnameVerifier that returns true. That makes the error go away by removing the check. Anyone on the network path can then impersonate the server and read or change the traffic, and these hacks tend to stay in code long after "just for testing". Importing one CA into a copy of the truststore fixes the actual problem and keeps verification on.
The missing intermediate case
Public sites are usually signed by an intermediate CA, which is signed by a root. The server is supposed to send the leaf and the intermediate. If it sends only the leaf, browsers can often fetch the intermediate themselves, but Java generally will not. So you get the same PKIX error even though the root is trusted.
I tested it: root, then Lab Intermediate CA, then a localhost leaf. One server (port 8444) sends only the leaf, another (port 8445) sends leaf plus intermediate.
openssl req -newkey rsa:2048 -nodes -keyout int.key -out int.csr -subj "/CN=Lab Intermediate CA" printf "basicConstraints=critical,CA:TRUE,pathlen:0\nkeyUsage=critical,keyCertSign,cRLSign\n" > int.ext openssl x509 -req -in int.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out int.crt -days 30 -extfile int.ext openssl req -newkey rsa:2048 -nodes -keyout leaf.key -out leaf.csr -subj "/CN=localhost" openssl x509 -req -in leaf.csr -CA int.crt -CAkey int.key -CAcreateserial -out leaf.crt -days 30 -extfile server.ext cat leaf.crt int.crt > leaf-fullchain.crt python server.py leaf.crt leaf.key 8444 # leaf only python server.py leaf-fullchain.crt leaf.key 8445 # leaf + intermediate s_client shows the difference right away. Leaf only:
Certificate chain 0 s:CN=localhost i:CN=Lab Intermediate CA Leaf plus intermediate:
Certificate chain 0 s:CN=localhost i:CN=Lab Intermediate CA 1 s:CN=Lab Intermediate CA i:CN=Lab Root CA With a truststore that holds only the root (root-only.p12, created with keytool -importcert -alias lab-root-only -file ca.crt -keystore root-only.p12 -storetype PKCS12 -storepass changeit -noprompt):
$ java -Djavax.net.ssl.trustStore=root-only.p12 -Djavax.net.ssl.trustStorePassword=changeit Client.java https://localhost:8444/ javax.net.ssl.SSLHandshakeException: (certificate_unknown) PKIX path building failed: ... $ java -Djavax.net.ssl.trustStore=root-only.p12 -Djavax.net.ssl.trustStorePassword=changeit Client.java https://localhost:8445/ HTTP 200 There are two fixes. The better one is on the server: configure it to send the full chain (with Let's Encrypt that is fullchain.pem). If you cannot change the server, import the intermediate into the truststore:
$ keytool -importcert -alias lab-intermediate -file int.crt -keystore root-only.p12 -storepass changeit -noprompt Certificate was added to keystore $ java -Djavax.net.ssl.trustStore=root-only.p12 -Djavax.net.ssl.trustStorePassword=changeit Client.java https://localhost:8444/ HTTP 200 The tell-tale sign of this case is in s_client: the chain has one certificate and its issuer is not a root you recognize.
The same fix inside IntelliJ with Keystore Workbench
Disclosure: I built this plugin. Keystore Workbench is a JetBrains IDE plugin from Datawharf Tools for viewing and editing Java keystores. The keytool fix above works fine on its own. This section is for people who would rather do it in the IDE.
Free:
- Tools | Open JDK cacerts opens the JDK truststore read-only (password
changeit), so you can check whether a CA is already in there. - Open keystores, see certificate details, expiry and the chain, and export public certificates.
Pro (US$19 per year for individuals, 30-day trial). Importing is Pro:
- Open the truststore, choose Allow editing, click Import, pick the
.pemfile, then Save. - Analyze | Verify Chain says when the chain is incomplete and names the missing issuer, which is the intermediate case above.
Two practical notes. The plugin makes no network connections, so you still get the certificates yourself with openssl s_client -connect host:443 -showcerts or by exporting them from the browser. And if the JDK folder is protected, copy cacerts to your project, edit the copy and start the program with the two -D flags, exactly as in step 3.
The lab above uses only OpenSSL, keytool and Java, so you don't need the plugin to follow it. The plugin is at https://plugins.jetbrains.com/plugin/36134-keystore-workbench.
Summary
- The error means the issuing CA is not in the truststore the JVM uses.
-
openssl s_client -connect host:443 -showcertsshows who issued the server certificate and whether an intermediate is missing. -
-Djavax.net.debug=ssl:trustmanagershows which truststore was used. - Copy
cacerts, runkeytool -importcerton the copy, and start the app with-Djavax.net.ssl.trustStoreand-Djavax.net.ssl.trustStorePassword. - Do not disable verification. If an intermediate is missing, fix the server or import the intermediate.


