GetResourceAsStream always returns null (Google App Engine)

I have a problem with my code and that it always throws NullPointerException:

public class WhateverResource extends ServerResource {
    @Get("json")
    public Representation represent(){
        InputStream is =  getContext().getClass().getClassLoader().getResourceAsStream("/whatever.properties");
        Properties props = new Properties();
        try {
            props.load(is); // NPE here!
            String whatever = props.getProperty("whatever_key");
            setStatus(Status.SUCCESS_OK);
        } catch (IOException e) {
            e.printStackTrace();
            setStatus(Status.SERVER_ERROR_INTERNAL);
        }
        return new StringRepresentation(props.toString());
    }
}

I check the WAR file generator and the target folder contains the file propertiesin the folder WEB-INF. What could be wrong with this code?

+4
source share
2 answers

The answer is as follows:

    InputStream is =  getContext().getClass().getResourceAsStream("/whatever.properties");

And GAE can read the stream without problems.

Without getClassLoader()

+4
source

Put the properties in the java source folder (src) in eclipse, it will be automatically copied to the class folder. Then the application can use it.

+1
source

Source: https://habr.com/ru/post/1569514/


All Articles