-2

I have a Java Method and read a file.

InputStream getFileStream(String fileName) {

    ClassLoader classLoader = getClass().getClassLoader();
    InputStream inputStream = classLoader.getResourceAsStream(fileName);
    
    .....
}

java throws strange error. Resources file can be read. But the file in folder in resource, file not found error. For example, file tree:

-- resources
--- file1.txt
--- folderA
---- file2.txt

filename is "file1.txt" no problem, but filename "folderA/file2.txt" file is not found.. I wonder what setting am I missing?

user5504616
  • 19
  • 1
  • 5
  • 2
    See the comments around using `ClassLoader` in a modular environment in this [resource lookup question](https://stackoverflow.com/questions/61531317/how-do-i-determine-the-correct-path-for-fxml-files-css-files-images-and-other), "It should also be noted that in modular applications, access to resources using ClassLoader.getResource() is, under some circumstances, subject to rules of strong encapsulation, and additionally the package containing the resource must be opened unconditionally.". Perhaps you miss an opens clause in your module-info.java. – jewelsea May 26 '22 at 21:34

0 Answers0