Classloader reads the file, but springboot cannot read it after jar package


jarClassloader reads the file, but springboot cannot read it after jar package/D:/test/test/.metadata/.plugins/org.eclipse.wst.server.core/test/test/test/WEB-INF/lib/test-0.0.1-SNAPSHOT.jar!/ca.crt

You may often use classloader in your project Getsystemresourceasstream and other methods to read the contents of a file, and use properties to read it.

But when you pack, you will find that there is a problem with your program. What should you do at this time?

solveYou can try the following code to get the file. The content can be modified by yourself. The logic is relatively simple, that is, get the relative address and then get the file

//The file name is FS + from cn

Why do you do this? After you package, you can use file f = new file (“above path – relative path”); FileNotFoundException will be found when getting the file

The file stream can be read through getresourceasstream() – it can only be read

Because this is not the format of the file resource locator (in jar, the resource has its special URL format: jar:! / {entry}).

If the class source code in the jar package uses file f = new file (relative path); The file resource cannot be found in the form of.

This work adoptsCC agreement, reprint must indicate the author and the link to this article