How to load resources in Java

We can load resources using getResource or getResourceAsStream method of ClassLoader.

Here is the example to load the test resource. Note that resource file is placed in the test resources directory.

Resource - Properties file

Resource – Properties file

Below code illustrates how to load above resource file in Java.

import java.util.Properties;

public class PropertyTest {
    public static void main(String[] args) {

        String resourceName = "";
        ClassLoader loader = Thread.currentThread().getContextClassLoader();
        Properties props = new Properties();
            InputStream resourceStream =
            System.out.println("webserver.url -> " + props.getProperty("webserver.url"));
        }catch (Exception ex){
            System.out.println("Exception ");

Here is the output of above code.
webserver.url ->

You may also like...