javanullgetresource

getResource on OpenJdk throws java.nio.file.NoSuchFileException


Application: Spring Boot,
Image: OpenJDK 8,
PaaS: Openshift,
Package: jar,
Error: No such file error. java.nio.file.NoSuchFileException
File Location: /servicename/src/main/resources/cert/trust.cer
Error Location: String certs = readFile((trustCertPath).getPath(), Charset.defaultCharset());
Error Location after Update 1: try (InputStream is = classloader.getResourceAsStream(resourcePath)) is null.

I have used cert/trust.cer as path. Since it did not work I gave it a try with /cert/trust.cer, trust.cer, ./cert/trust.cer. I was able to run this class on local using Windows (/cert/trust.cer) but fails from command line and also fails when deployed obviosly.

Update 1: I am using getResourceAsStream(cert/trust.cer). Inputstream is results to null.

public class CertsUtility implements InitializingBean {
public static final Logger logger = LoggerFactory.getLogger("CertsUtility");
private String keystorePaaS;
private String keystorePass;
private String CertPath = "cert/trust.cer";

public void setKeystorePaaS(String keystorePaaS) {
    this.keystorePaaS = keystorePaaS;
}

public void setKeystorePass(String keystorePass) {
    this.keystorePass = keystorePass;
}

static File getPath(String path) {
    URL url = CertsUtility.class.getClass().getResource(path);
    if (url != null) {
        File file = new File(url.getPath());
        return file;
    } else {
        File file = new File(path);
        return file;
    }
}

static String getResourceFileAsString(String resourcePath) throws IOException {
    ClassLoader classloader = ClassLoader.getSystemClassLoader();
    try (InputStream is = classloader.getResourceAsStream(resourcePath)) {
        if (is == null)
            return null;
        try (InputStreamReader isr = new InputStreamReader(is)) {
            BufferedReader reader = new BufferedReader(isr);
            String targetString = reader.lines().collect(Collectors.joining());
            return targetString;
        }
    }

}

void genIndividualandLoad() throws KeyStoreException, FileNotFoundException, NoSuchAlgorithmException,
        CertificateException, IOException, InterruptedException {
    try {
        KeyStore keyStore = KeyStore.getInstance("JKS");
        InputStream fileInputStream = new FileInputStream(keystorePaaS);
        keyStore.load(fileInputStream, keystorePass.toCharArray());
        fileInputStream.close();
        String certs = readFile((trustCertPath).getPath(), Charset.defaultCharset());
        String[] certificates = certs.split("(?<=-----END CERTIFICATE-----\n)");

        for (int i = 0; i < certificates.length - 1; i++) {
            String individualName = getPath(CertPath).getParent() + i + ".cer";
            try (FileOutputStream outputStream = new FileOutputStream(individualName)) {
                byte[] strToBytes = certificates[i].getBytes();
                outputStream.write(strToBytes);
                try (InputStream inStream = new FileInputStream(individualName)) {
                    CertificateFactory cf = CertificateFactory.getInstance("X.509");
                    X509Certificate cert = (X509Certificate) cf.generateCertificate(inStream);
                    keyStore.setCertificateEntry("" + i, cert);
                }
                FileOutputStream fileOutputStream = new FileOutputStream(getPath(keystorePaaS));
                keyStore.store(fileOutputStream, keystorePass.toCharArray());
                fileOutputStream.close();
                outputStream.close();
            }
        }
    } catch (KeyStoreException e) {
        logger.error("| genIndividualandLoad() | Keystore exception occurred", e);
    } catch (FileNotFoundException e) {
        logger.error("| genIndividualandLoad() | File not found exception occurred", e);
    } catch (NoSuchAlgorithmException e) {
        logger.error("| genIndividualandLoad() | Algorithm related exception occurred", e);
    } catch (CertificateException e) {
        logger.error("| genIndividualandLoad() | X.509 Certificate exception occurred", e);
    } catch (IOException e) {
        logger.error("| genIndividualandLoad() | I/O exception occured", e);
    }
}

public void afterPropertiesSet() throws KeyStoreException, FileNotFoundException, NoSuchAlgorithmException,
        CertificateException, IOException, InterruptedException {
    genIndividualandLoad();
}

}


Solution

  • I used ClassPathResource from org.springframework.core.io.ClassPathResource to solve this problem.