Application works in intelij but not after build

Viewed 50

My application works fine in intelij, but when I build and run as a minecraft mod, it gives this error.

[21:55:17] [main/INFO]: [STDERR]: 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

[21:55:17] [main/INFO]: [STDERR]: Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

[21:55:17] [main/INFO]: [STDERR]: Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

The webserver is running, and it accepts input sent while running inside intelij, and through insomnia (rest client), so the issue is definitely on the client side.

Here is the code for the request I am sending.

private void sendPost(String url, String postParams) throws Exception {
    URL obj = new URL(url);
    HttpURLConnection conn = (HttpURLConnection) obj.openConnection();

    conn.setUseCaches(false);
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Host", "https://subdomain.website.com/api");
    conn.setRequestProperty("User-Agent", USER_AGENT);
    conn.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
    conn.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
    conn.setRequestProperty("Connection", "keep-alive");
    conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    conn.setRequestProperty("Content-Length", Integer.toString(postParams.length()));

    conn.setDoOutput(true);
    conn.setDoInput(true);

    // Send post request
    DataOutputStream wr = new DataOutputStream(conn.getOutputStream()); // getting the error here
    wr.writeBytes(postParams);
    wr.flush();
    wr.close();

    // handle input
}

And this is how the url is prepared to pass to the previous function

StringBuilder result = new StringBuilder();
    for (String param : new String[] {"param1" + "=" + URLEncoder.encode(param1, "UTF-8"), "param2" + "=" + URLEncoder.encode(param2, "UTF-8"), "param3" + "=" + URLEncoder.encode(param3, "UTF-8")}) {
        if (result.length() == 0) {
            result.append(param);
        } else {
            result.append("&").append(param);
        }
    }
String postParams = result.toString();

this.sendPost(url, postParams); // url is a constant
1 Answers

The exception is because your client does not trust the server. In practice this means the trust store used by your client JVM does not contain a public certificate for the server or a signing authority (CA) in the trust chain.

As to why it works in Intellij and not externally, it's likely you are using a different JRE/JDK or a different trust store.

Related