Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!
-
getResource
andgetResourceAsStream
tries to find files in theresources
directory undersrc/main/resources
orsrc/test/resources
(depending on whether you are running the application or tests for the application). If the specified file is not found,null
is returned (which might cause NullPointerExceptions if you make a mistake specifying the resource to use). -
getClass.getResource
requires a leading/
in the file name to find.getClass().getResource("/some_file_to_find")
Another way is to use
getClass().getClassLoader().getResource
, and you must not have a leading/
for files.getClass().getClassLoader().getResource("some_file_to_find")
-
getResource
returns an URL object. You can use thegetFile
orgetPath
method to get the path of the file. Both of these 2 methods return a String.