Background
The springboot project calls the method of a local jar package and needs the file path under the project resources. The file is stored under resources.
Trial and error process
1. Original code:
First use classloader.getsystemresource ("file name")
, and the local operation is normal. However, an error null pointer is reported after the jar package is typed.
2. First modification:
Most people on the Internet say that they use this. Getclass(). Getclassloader(). Getresource()
, but they still can’t. the two principles are the same.
3. Second modification:
Another method is to use the file stream to obtain the file, this. Getclass(). Getclassloader(). Getresourceasstream()
, but this is to obtain the file directly. It needs to be the file path, but it can’t.
4. Finally:
Use string filepath = system. Getproperty ("user. Dir") + "\ \ file name"
, get the path of the jar package, add the file name to get the file address, but you need to put the file in the same file directory as the jar package.
dot
system. Getproperty (“user. Dir”) + “\ double slash in file name” is in Windows environment. If it needs to be changed to “/” in Linux environment.
(at present, we haven’t found a way not to put the documents outside. There is a solution. I hope the bosses will give us their advice!)
Read More:
- Zip to decompress the jar package, like the add file in the jar package
- How to select the jar package version for Maven package when the jar package versions referenced by multiple modules are inconsistent
- After unity connects with androidstudio, unity packages APK with an error: commandinvocationfailure: failed to re package resources
- Unity package APK exception failed to re package resources
- Solution to null pointer error in array
- Does the version of idea2020.1 import mybayis jar package or report an error? Error: (4,28) Java: package org.apache.ibatis.io does not exist
- When Maven hits the jar package, an error is reported when executing install, and the symbol cannot be found in a line
- [solution] the resource file cannot be found in the jar package of Java
- The @ Autowired annotation in springboot is invalid in ordinary classes. How to solve and use the null pointer exception java.lang.nullpointerexception
- Local can link to the server database, upload jar package to the server will report an error
- The introduction of third-party jar package in mavn project results in classnotfoundexception
- An error occurred when running ROCR: Failed to load the package ‘caTools’, package or namespace load failed
- When the spring MVC project is running on idea, an error is reported when the controller is a null pointer
- How to turn your project into a jar package in idea
- The remote port occupation of Linux startup jar package script reports an error
- Solution of Java resources error reporting
- [problem solving] Error:failed to get canonical path of /cow
- Solve the interceptor calling service layer to display null pointer.
- Java – read all the files and folders in a certain directory and three methods to get the file name from the file path
- kubectl get csr No resources found.