Spring boot cannot read root path and file after jar

Time:2020-6-27

Take a notegetContextClassLoader().getResource()Pit ofFileNotFoundException

After deploying the code to the test environment this morning, it’s time to test the requirementsFileNotFoundException, it was found that an error was reported when getting the font path. It’s strange that this problem was not found during local debugging.
Error log:

Caused by: java.io.FileNotFoundException: /opt/supplier_web/supplier_web.jar!/BOOT-INF/lib/supplier_web.web-0.0.1.jar!/font/simsun.ttf (No such file or directory)

Spring boot cannot read root path and file after jar

The problem code is as follows:

Spring boot cannot read root path and file after jar

The troubleshooting found the following problems:

This is because spring attempts to access the file system path after packaging, but cannot access the path in the jar.

terms of settlement:

useClassPathResourcesolve the problem.
Spring boot cannot read root path and file after jar