ClassLoader is loading the wrong file

I use this code snippet to get the file as input stream. The version.txt file is packaged in my jar application, in the topmost folder.

InputStream resource = getClass().getClassLoader().getResourceAsStream("version.txt");

It works almost all the time. But for one user, he picks up another version.txt file, which is not in my bank. How can I guarantee that this downloads the specific version.txt file that is in my bank?

+3
source share
3 answers

" ", ? , , JAR , , version.txt, Java , .

, JAR , version.txt , , :

com.yourcompany.yourproject.version

, :

Stream resource = getClass().getClassLoader().getResourceAsStream("com/yourcompany/yourproject/version/version.txt");

- -.

+9

, , , . (, log4j, ), , .

version.txt , . com.myapp( ),

getClass().getClassLoader().getResourceAsStream("com/myapp/version.txt");
+3

the code snippet will use the class loader loading this class to find the version.txt file. if the file exists in the class path used by the class loader in more than one place, it may return the wrong file (depending on the order of the class path).

0
source

Source: https://habr.com/ru/post/1718898/


All Articles