A Java application that rejects a valid certificate usually fails with the same cryptic line, “PKIX path building failed: unable to find valid certification path to requested target”, while browsers, curl and online SSL checkers all report that the certificate is fine. Often the certificate really is fine. What fails is Java’s own trust decision, which uses different data and stricter rules than a desktop browser. This article explains why Java applications reject certificates that look valid elsewhere, how to find the cause in a few minutes and how to fix it without turning off validation.
Why a Green Padlock Proves Very Little
Many people believe that if the browser accepts a certificate, every client will. That is wrong, and the gap is bigger than most people expect.
Browsers quietly fix broken setups. Chrome and Edge download missing intermediate certificates from the AIA URL embedded in the leaf certificate. Firefox ships a preloaded list of known intermediates. Java does neither by default. The JDK can fetch AIA intermediates through the system property com.sun.security.enableAIAcaIssuers=true, but it is off unless someone turns it on.
The trust store is also different. Browsers use the operating system’s root store or their own, such as the Chrome Root Store or Mozilla NSS. Java ships its own file at $JAVA_HOME/lib/security/cacerts, and that file only changes when the JDK is updated. curl on the same host reads the OS bundle, so “curl works from the app server” tells you nothing about Java’s trust store.
Here is a common case. A nightly integration job that calls a partner’s API starts failing the night after the partner renews its certificate. Chrome shows a padlock and curl works. The Java job throws PKIX errors. Almost always, one of two things happened. Either the new chain is missing an intermediate, or the new certificate chains to a root that the JDK doesn’t know, for example because the container image was built years ago on an old Java 8 base.
What Actually Causes Java to Reject Valid Certificates
The server sends an incomplete chain. This is the most common cause. The server sends only the leaf certificate, or the leaf plus the wrong intermediate. Browsers recover from this and Java doesn’t. The fix belongs on the server, and the background in how intermediate certificates and chains work explains why the order and completeness of the chain matter.
The JDK trust store is outdated. ISRG Root X1, the Let’s Encrypt root, only arrived in Java 8 with update 141. Long-lived Docker images, vendor appliances with a bundled JRE and legacy application servers often run builds older than that. Roots also get removed or expire. See how root store updates affect your certificates.
A custom trust store replaced the default one. A flag like -Djavax.net.ssl.trustStore=/opt/app/internal-ca.jks does not add to cacerts. It replaces cacerts completely. Usually someone adds this flag so the app can reach an internal database or message broker. Internal calls keep working and every call to a public HTTPS API breaks.
Algorithm constraints and distrust policies block the chain. The java.security file contains jdk.certpath.disabledAlgorithms, which blocks MD5, RSA keys under 1024 bits and SHA-1 signatures on TLS server chains anchored in JDK roots. In those cases the error says “Algorithm constraints check failed” instead of PKIX. The JDK also enforces its own CA distrust decisions, separately from browsers. JDK releases from late 2024 onward reject TLS certificates that chain to Entrust roots and were issued after the distrust cutoff date. A trust store that looks complete can still reject a certificate because of these rules.
The hostname check fails. Java checks the Subject Alternative Name field and ignores the Common Name whenever a SAN is present. If clients connect by IP address, the certificate needs an iPAddress SAN entry. A DNS entry for the same address does not count. Java also sends SNI only for hostnames, never for IP literals. Some old startup scripts still set jsse.enableSNIExtension=false. Without SNI, a server with several sites returns its default certificate. The background is in what SNI is and why it matters.
The clock is wrong. A CertificateNotYetValidException right after renewal usually means the VM’s clock has drifted, not that the certificate is bad.
Diagnosing the Failure in About Ten Minutes
1. Find the root cause at the bottom of the stack trace. SunCertPathBuilderException means Java could not build a path to a trusted root, so the problem is the chain or the trust store. “Algorithm constraints” means a policy blocked it. “No subject alternative names matching” means the hostname check failed.
2. Check what the server actually sends: openssl s_client -connect api.example.com:443 -servername api.example.com -showcerts. If only one certificate comes back and its issuer is not a root, the chain is incomplete.
3. Run the client with -Djavax.net.debug=ssl:handshake:verbose. The output shows which trust store was loaded and every certificate the server presented.
4. Check whether the expected root is in that store: keytool -list -cacerts -storepass changeit | grep -i isrg. The -cacerts shortcut needs JDK 9 or later. On Java 8, pass the full path with -keystore.
5. Confirm which JVM the service really runs on. Check from inside the container or the process list, not on your workstation. A host can have JDK 21 installed while the systemd unit starts a bundled Java 8 runtime.
Fixing It at the Right Layer
If the chain is incomplete, fix the server, not the clients. With Certbot, point nginx’s ssl_certificate at fullchain.pem instead of cert.pem. That one change fixes every Java, Python and Go client at once.
If the trust store is old, upgrade the JDK. Oracle and OpenJDK vendors ship updated cacerts in the quarterly Critical Patch Updates (January, April, July and October). On Debian and Ubuntu, the ca-certificates-java package keeps /etc/ssl/certs/java/cacerts in sync with the OS bundle. Eclipse Temurin container images can import mounted certificates at startup when USE_SYSTEM_CA_CERTS=1 is set.
If you need an internal CA, merge it into a copy of the default store instead of replacing the store. Copy cacerts, import your root with keytool -importcert -keystore app-trust.p12 -storetype PKCS12 -alias corp-root -file corp-root.pem, and point the application at the merged file. The file then contains both the public roots and your internal one.
For hostname and algorithm failures, reissue the certificate. Add the correct SANs, use SHA-256 signatures, and use RSA 2048 or ECDSA P-256 keys. Re-enabling SHA-1 in java.security should only ever be a short, documented exception for an isolated legacy system.
Shortcuts That Make Things Worse
The trust-all TrustManager copied from an old forum answer is the worst option. The same goes for NoopHostnameVerifier in Apache HttpClient. Both are meant to be temporary, both end up in production, and both quietly remove protection against man-in-the-middle attacks.
Importing the leaf certificate into cacerts is a slower failure. It works until the next renewal. The CA/Browser Forum cut maximum validity to 200 days in March 2026, and it drops to 100 days in 2027 and 47 days in 2029, so that next renewal comes soon. Import the root or the issuing CA, never the leaf.
Editing the JDK’s own cacerts file is also fragile. The next package upgrade replaces $JAVA_HOME and your change disappears without warning. Keep custom trust material in a separate file that your configuration management owns.
Edge Cases Outside the Standard Advice
In air-gapped or tightly firewalled networks, AIA fetching can’t reach the internet, so enabling it does nothing. If you have turned on revocation checking with com.sun.net.ssl.checkRevocation, connections may fail with “Could not determine revocation status” because OCSP responders are unreachable. In that case, use a mirrored CRL or soft-fail revocation checking that you have deliberately chosen.
Android apps are a different case. They use the platform trust store and Network Security Config, not cacerts, so most of the fixes above don’t apply there.
Catching It Before Java Does
Most of these failures could have been caught on the day of renewal. Check the chain the server actually sends, not just the leaf’s expiry date, and you catch the most common cause before any client hits it. For critical integrations, add one more check: a small Java health check that runs on the same JDK build as production and calls the endpoint right after every certificate rotation. If it fails, you learn about the problem before the nightly job or a customer does.
