Cannot load keystore when running java -jar *.jar spring boot application

See original GitHub issue

Hi,

I have a kafka-keytore.jks file in src/main/resources and when I run the application with mvn spring-boot:run there are no exceptions but when I run the application with java -jar target/demo-0.0.1-SNAPSHOT.jar the following error occurs:

Caused by: java.io.FileNotFoundException: class path resource [kafka-keytore.jks] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/Users/aalmazan/demo/target/demo-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/kafka-keytore.jks at org.springframework.util.ResourceUtils.getFile(ResourceUtils.java:217) ~[spring-core-5.0.7.RELEASE.jar!/:5.0.7.RELEASE] at org.springframework.core.io.AbstractFileResolvingResource.getFile(AbstractFileResolvingResource.java:133) ~[spring-core-5.0.7.RELEASE.jar!/:5.0.7.RELEASE] at org.springframework.boot.autoconfigure.kafka.KafkaProperties$Ssl.resourceToPath(KafkaProperties.java:945) ~[spring-boot-autoconfigure-2.0.3.RELEASE.jar!/:2.0.3.RELEASE] … 76 common frames omitted

If I unpack target/demo-0.0.1-SNAPSHOT.jar the kafka-keytore.jks.jks file is inside.

Here are the steps to reproduce the problem:

  • Go to and generate new project skeleton with spring boot 2.0.3, demo artifact name and add Kafka dependency.
  • Add the following properties to src/main/resources/application.properties file:
spring.kafka.ssl.key-password=testkey
spring.kafka.ssl.keystore-location=classpath:kafka-keytore.jks
spring.kafka.ssl.keystore-password=teststore
  • Execute mvn clean install
  • Execute mvn spring-boot:run -> no error occurs
  • Execute java -jar target/demo-0.0.1-SNAPSHOT.jar -> the error occurs

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

12reactions
garyrussellcommented, Jun 18, 2018

One possible workaround is to extract the keystore to a temporary file:

@SpringBootApplication
public class Kgh710Application {

	public static void main(String[] args) throws Exception {
		FileCopyUtils.copy(new ClassPathResource("client.ks").getInputStream(),
				new FileOutputStream("/tmp/client.ks"));

		SpringApplication.run(Kgh710Application.class, args);
	}

}

and

spring.kafka.ssl.keystore-location=file:/tmp/client.ks
1reaction
garyrussellcommented, Feb 20, 2020

Yes, that should work ok for you. I don’t think there is any other choice, given the limitations of the Kafka clients.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Spring Boot not loading keystore specified in application.yml
When I attempt to execute the application, it starts but quickly fails because it cannot load a keystore needed for SSH/TLS secure ...
Read more >
Why am I not able to start a Java application with SSL?
I am trying to set up a simple Spring application to use SSL and host it on Digital Ocean. Why is my app...
Read more >
Spring Boot Reference Guide
Executable jars (sometimes called “fat jars”) are archives containing your compiled classes along with all of the jar dependencies that your code needs...
Read more >
Spring Boot Reference Documentation
You can use Spring Boot to create Java applications that can be started by using java -jar or more traditional war deployments. We...
Read more >
“How-to” Guides - Spring
In a running application with actuator enabled, look at the conditions ... example shows setting SSL properties using a Java KeyStore file:.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found