Search code examples
javaspring-bootsslkeystorespring-kafka

Jar file not able to find the keystore


I am running a SpringKafka producer.I have configured the keystore location in the yml file and its being picked up when ran in eclipse. But when the code is run as a jar its unable to find the keystore location.How to resolve this.

spring:
  kafka:
    producer:
      ssl:
        key-password:
        key-store-location: classpath:es-cert.jks
        key-store-password: password
        key-store-type: jks

This is my yml file.

I am getting the following error:

java.io.FileNotFoundException: class path resource [es-cert.jks] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/u01/home/app/user-docker/kafka2.jar!/BOOT-INF/classes!/es-cert.jks

But the es-cert.jks is present inside jar.


Solution

  • Kafka doesn't understand Spring resources only files on the file system.

    You can't use classpath: to reference the key store from a jar, since Spring Boot has to convert the resource to an absolute path to pass it to Kafka.

        private String resourceToPath(Resource resource) {
            try {
                return resource.getFile().getAbsolutePath();
            }
            catch (IOException ex) {
                throw new IllegalStateException("Resource '" + resource + "' must be on a file system", ex);
            }
    

    One solution is to copy the keystore yourself from the jar resource to a file on the file system. Do this before loading the SpringApplication.

    e.g. use FileCopyUtils.