Load static resources

Hi everybody,

I need to load some information from a *.properties file but I can’t load file. Reading docs I thought it should be simple but I don’t know why it isn’t.

try {
            File file = new File("src/main/resources/config.properties");
            InputStream input = new FileInputStream(file);
 
            Properties prop = new Properties();
            
            prop.load(input);
            
} catch (Exception ex) {
            ex.printStackTrace();
        }

This code gives me this error

java.io.FileNotFoundException: src/main/resources/config.properties (No such file or directory)

I tried other locations but without any success.

Could someone give me an hint?

Thank you very much.

Matteo

If you put files in “src/main/resources” folder, it will get packaged. So, you can simply get it via the Class Loader.

InputStream input = getClass().getClassLoader().getResourceAsStream("config.properties");

Thank you very much, it works!!