0

I have a Spring Boot application which is to be deployed as a .war on Cloud Foundry. It has many properties files which are kept in src/main/resources/ folder. The application uses custom libraries as dependencies which reads these properties.

For example: one of the dependencies is a custom logger library and it reads the log.properties file at runtime. Now most of these libraries use the ClassLoader.getSystemresource().getFile() function to read the properties. Since it is a dependency, I can't edit the libraries.

Everything works locally while testing via STS. But when the .war file is generated and deployed, it is unable to find any of these resource files and hence fails. All the property files are present in the .war and if I use Spring's ClassPathResource() methods to access the files, it is accessible.

I have seen similar questions on Stack Overflow which advise to read files as streams instead of file and other similar solutions but none of this will work in my case since I will not be able to modify the dependency source code. Is there any way to achieve this?

My problem is very similar to this question Classpath resource not found when running as jar except that I wont be able to apply the solution given.

Edit

As requested below is the code snippet

File logfile = ClassLoader.getSystemResource("log.properties").getFile();

This does not work. Where as

File logFile = new ClassPathResource("log.properties").getFile();. works. But i need the first code snippet to work since it is inside a jar and i can't modify it.

jubin
  • 153
  • 5
  • 15
  • Posting your code for loading resources would be really helpful – Ivan Pronin Aug 07 '17 at 19:12
  • Agreed, please post the code snippet, we need to see exactly how it is attempting to access files from the classpath. – Ben M Aug 07 '17 at 19:51
  • If it is possible, add the files to the classpath of start up program classpath.I dont know about Cloud Foundry, but in Tomcat - I could add an external folder to classpath when tomcat is starting and put the files in the folder and then it should work. This is because getSystemResource looks for file in System Classloader – Hima Aug 08 '17 at 07:33
  • @Hima the reason it is not working is what you said. But adding the files to classpath of startup doesn't work. Instead i ended up modifying the jar itself with a minor change – jubin Aug 08 '17 at 18:59
  • Glad it worked out. – Hima Aug 09 '17 at 04:35

1 Answers1

0

I ended up modifying the jar with a minor change. The issue occurs because ClassLoader.getSystemResource looks for file in System Classloader where as the files are zipped inside the war. I modified

File logfile = ClassLoader.getSystemResource("log.properties").getFile();

to

File logfile = this.getClass().getClassLoader().getSystemResource("log.properties").getFile();

and it started working.

jubin
  • 153
  • 5
  • 15