Skip to content

Can't find the location of file keystore when running as jar #9089

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
momo159 opened this issue May 4, 2017 · 16 comments
Closed

Can't find the location of file keystore when running as jar #9089

momo159 opened this issue May 4, 2017 · 16 comments
Labels
status: feedback-provided Feedback has been provided

Comments

@momo159
Copy link

momo159 commented May 4, 2017

I use Spring Boot 1.4.0. And I find that tomcat can't read the keystore conatined in a jar. But how can I load the keystore in a jar.

The configuration about SSL:

server:
  port: 8443
  ssl:
    enabled: true
    key-store: classpath:keystore.jks
    key-store-password: pass
    key-alias: pass
    #protocol: TSL
    enabled-protocols: TLSv1,TLSv1.1,TLSv1.2

The Exception:

java.io.FileNotFoundException: class path resource [xx.jks] cannot be resolved to absolute file path because it does not reside in the file system:jar:file:/C:/xx/xx/xx/xx/xx/target/xx-1.0-SNAPSHOT.jar!/xx.jks

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label May 4, 2017
@wilkinsona
Copy link
Member

This works fine in our Tomcat SSL sample. Please try with the latest 1.4.x release (1.4.6.RELEASE) and, if that doesn't help, please provide a small sample that reproduces the problem.

@wilkinsona wilkinsona added the status: waiting-for-feedback We need additional information before we can continue label May 4, 2017
@momo159
Copy link
Author

momo159 commented May 4, 2017

it works fine in IntelliJ IDEA. If I build it as a jar and run this jar, it will show this Exception

@wilkinsona
Copy link
Member

wilkinsona commented May 4, 2017

it works fine in IntelliJ IDEA. If I build it as a jar and run this jar. It will show this Exception

I already understood that from your original description when you said it can't read the keystore from a jar file. As I said above, it works fine in our sample so we're going to need to see a sample from you that reproduces the problem.

@wilkinsona wilkinsona added status: waiting-for-feedback We need additional information before we can continue and removed status: waiting-for-feedback We need additional information before we can continue labels May 4, 2017
@momo159
Copy link
Author

momo159 commented May 4, 2017

Thanks. I fixed it by your samples.

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels May 4, 2017
@momo159 momo159 closed this as completed May 4, 2017
@snicoll snicoll removed the status: waiting-for-triage An issue we've not yet triaged label Jun 9, 2017
@GursharanKhalsa
Copy link

GursharanKhalsa commented Oct 31, 2018

@wilkinsona
I'm facing the same problem and I've already followed your SSL sample but it doesn't work for me.
In elicpse it works fine but if I build its as a JAR then it will throw an exception.

Properties:

server.port=1019
server.ssl.enabled=true
server.ssl.key-alias=springboot_pk
server.ssl.key-store-type=JKS
server.ssl.key-password=springboot
server.ssl.key-store=classpath:springboot.JKS

Exception:

elling refresh attempt: org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.boot.web.server.WebServerException: Could not load key store: class path resource [springboot.JKS] cannot be resolved to URL because it does not exist
2018-10-31 11:10:05.823  INFO 9736 --- [           main] ConditionEvaluationReportLoggingListener 
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2018-10-31 11:10:05.829 ERROR 9736 --- [           main] o.s.boot.SpringApplication               : Application run failed
org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.boot.web.server.WebServerException: Could not load key store: class path resource [springboot.JKS] cannot be resolved to URL because it does not exist
        at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:155) ~[spring-boot-2.0.4.RELEASE.jar!/:2.0.4.RELEASE]

@wilkinsona
Copy link
Member

wilkinsona commented Oct 31, 2018

@GursharanKhalsa Please see my comments above. If you’d like someone to spend some time trying to help you, please provide a small sample that reproduces the problem. Before doing that, I’d check that you have a file named springboot.JKS (with matching case) in src/main/resources.

@GursharanKhalsa
Copy link

@wilkinsona Thanks for the reply but this problem occur because of classpath resource when I run in eclipse it would easily find the .jks file but after building the project into single.jar. The .jks file would not find as a classpath resource so I resolved it by replacing classpath:springboot.jks to ./springboot.jks and place .jks file parallel to .jar file and it worked.

But I would like to know about if we add some required resources in src/main/resources parallel to application.properties then why spring throws these kinds of exceptions at runtime after build and What is an appropriate solution to add the resources which we need at runtime?

@wilkinsona
Copy link
Member

@GursharanKhalsa Loading a keystore from a jar file should work. As I’ve already said above, if you’d like someone to spend some time trying to help you, please provide a small sample that reproduces the problem.

@Pmenasigi

This comment has been minimized.

@wilkinsona

This comment has been minimized.

@HimanshuMamodiya
Copy link

@wilkinsona Thanks for the reply but this problem occur because of classpath resource when I run in eclipse it would easily find the .jks file but after building the project into single.jar. The .jks file would not find as a classpath resource so I resolved it by replacing classpath:springboot.jks to ./springboot.jks and place .jks file parallel to .jar file and it worked.

But I would like to know about if we add some required resources in src/main/resources parallel to application.properties then why spring throws these kinds of exceptions at runtime after build and What is an appropriate solution to add the resources which we need at runtime?

I am also facing the same issue

@onlyhuman
Copy link

I think it is a bit more general problem, try to load a resource from a spring boot jar as an URL instead of a File. Like this way:
@Value("${server.ssl.key-store}") private Resource keyStore;
Then later:
keyStore.getURL()

@Brice-D
Copy link

Brice-D commented Jan 21, 2021

i had the same problem and i just solved it like that:
server.ssl.key-store=src/main/resources/keystore.p12

no "/" for the "src"

@wilkinsona
Copy link
Member

wilkinsona commented Jan 21, 2021

Thanks for trying to help out, @Brice-D. Please be aware that pointing to src/main/resources will only work when running the app with the source code available and the root of the source repository as the current working directory. It won't work if you launch the app from another directory or you deploy the jar to another machine.

@Brice-D
Copy link

Brice-D commented Jan 21, 2021

Thanks.
It's working now for some reason
Screenshot 2021-01-21 at 13 12 07
with the configuration from your sample

@priyanka28-lang
Copy link

priyanka28-lang commented Jan 30, 2021

My application files has :

stargate-filter.truststore=standard_trusts.jks

and I can't even change the code for getResource as I am importing some dependency ..

ks = KeyStore.getInstance(KeyStore.getDefaultType());
URL resource = this.getClass().getClassLoader().getResource(config.getTruststore());
if(resource == null) { 
    System.out.println("Could not find the truststore located at [" + config.getTruststore()+ "]");
} else {
    File file = new File(resource.toURI());
    FileInputStream fis = new FileInputStream(file);
    ks.load(fis, config.getTruststorePassword().toCharArray());
}

In my local it works fine, but deploying it to any external server (k8) I get this as URI is not hierarchical .. and if i copy it o some location and provide the path .. I get exception "System.out.println("Could not find the truststore located at [" + config.getTruststore()+ "]");"...

Any suggestions ..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: feedback-provided Feedback has been provided
Projects
None yet
Development

No branches or pull requests

10 participants