Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!
-
getResourceandgetResourceAsStreamtries to find files in theresourcesdirectory undersrc/main/resourcesorsrc/test/resources(depending on whether you are running the application or tests for the application). If the specified file is not found,nullis returned (which might cause NullPointerExceptions if you make a mistake specifying the resource to use). -
getClass.getResourcerequires 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") -
getResourcereturns an URL object. You can use thegetFileorgetPathmethod to get the path of the file. Both of these 2 methods return a String.