blue-sky blue-sky - 3 months ago 21
Scala Question

Receiving sun.security.provider.certpath.SunCertPathBuilderException (Scala) after installing cert

I'm attempting to download the content of https://slashdot.org/ with this code :

scala.io.Source.fromURL("https://slashdot.org/", "ISO-8859-1").mkString


This throws exception :

Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at sun.security.validator.PKIXValidator.doBuild(Unknown Source)
at sun.security.validator.PKIXValidator.engineValidate(Unknown Source)
at sun.security.validator.Validator.validate(Unknown Source)


To fix I export the certificate from https://slashdot.org/ via Chrome. I assign alias 'slashdot' and name the vert file 'cert'. I install using command

keytool -import -trustcacerts -keystore cacerts -storepass changeit -noprompt -alias slashdot -file cert.cer
The certificate is imported successfully but same exception is thrown when I attempt to save URL :

scala.io.Source.fromURL("https://slashdot.org/", "ISO-8859-1").mkString


Have I missed a step installing the cert ?

Do I need to specify the "Certification path" as part of downloading the content as specified for the https://slashdot.org/ cert :

enter image description here

Update :

I thought this could be related to my Scala setup but Java code below throws same error :

import java.io.InputStream;
import java.nio.file.Files;
import java.net.URL;
import java.nio.file.StandardCopyOption;
import java.nio.file.Path;

public class download {
public static void main(String args[]) throws Exception{
URL website = new URL("https://slashdot.org/");
Path target = new java.io.File("c:\\download\\myfile.txt").toPath();

try (InputStream in = website.openStream()) {
Files.copy(in, target, StandardCopyOption.REPLACE_EXISTING);
}
}
}

Answer

I'm not 100% sure why I needed to explicitly set the path of cert file but this works :

System.setProperty("javax.net.ssl.trustStore", "C:\\java\\jdk1.8.0_65\\jre\\lib\\security\\cacerts");

The comments offered to this question attributed to me trying this solution.