I have a unit test package in which I keep a few txt files. These get loaded via getClass().getResource(file); call and it works just fine. I added into the same folder a csv file, and if I supply its name as the parameter i.e. getClass().getResource("csvFile.csv"); I get null… any ideas why?
I have a unit test package in which I keep a few txt files.
Share
When you use
it looks relative to the class.
When you use
it looks in the top level directories of your class path.
I suspect you want the second form.
From Class.getResource(String)
As you can see the directory translation of the package name of the class is used.
For example, I have a maven project where the code is under
src/main/java. My resources directorysrc/main/resourcesI add
csvFile.csvto my resources directory which will be copied to my class path.which prints
This is in the area built by maven from the resources directory.