I wrote a small java tool at random. After the maven package was successful, I found that the tool could not read the file information in the jar package. The file to be read is located in the /src/main/resources directory. After the package is successful, the file will be In the root directory of the jar package, I always thought that the path was wrong, and then used this.getClass().getClassLoader().getResource("").getPath(); to find that the path was in the form of xxx.jar!/com/demo/.. , the path always gets wrong.
Then I tried BufferedReader in = new BufferedReader( new InputStreamReader( this.getClass().getClassLoader().getResourceAsStream(path))); to get the file content.
BufferedReader in = new BufferedReader(new InputStreamReader(this.getClass().getClassLoader().getResourceAsStream(path))); StringBuffer buffer = new StringBuffer(); String line = ""; while ((line = in.readLine()) != null){ buffer.append(line); } String input = buffer.toString();