Jar file not able to find the keystore

时光怂恿深爱的人放手 提交于 2021-01-29 12:49:37

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/58712289/jar-file-not-able-to-find-the-keystore

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!