简体   繁体   中英

java access resource files from class loaded by another classloader

I have a class which needs to do some https request.
Just in case the ssl truststore and keystore are not set, the jar which contains my class, has them in its resource folder.
My class is being dynamically loaded by some classloader I have no control about and now when I do

getClass().getClassLoader().getResourceAsStream("keystore.jks");

the resource is not being found.
My theory on this is that getClassLoader returns the classloader returns the classloader from a webserver, which probably isn't able to find the resource keystore.jks in the jar file.
Any help is appreciated.

I found a way around the problem. I can use MyClass.class.getResourceAsStream("/keystore.jks"); to load the resource properly.
I am not exactly sure why it works though. When I was using MyClass.class.getClassLoader().getResourceAsStream("keystore.jks"); it wasn't working. Although I think I read somewhere that Class.getResource() with a leading / is the same as ClassLoader.getResource() without one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM