How do I load a properties file from WEB-INF/classes?

George McKinney

We use a number of properties files, both from the classes directory and from sub directories without any problem.

For files directly in the classes directory, our code is like:

        Properties dbProps = new Properties();

        //The forward slash "/" in front of in_filename will ensure that
        //no package names are prepended to the filename when the Classloader
        //search for the file in the classpath
        InputStream is = getClass().getResourceAsStream("/"+in_filename);
        if(null == is)
            throw new ConfigException("Can't locate file:" +in_filename);
            dbProps.load(is);//this may throw IOException
            return dbProps;
        catch (IOException ioe)
            System.err.println("Properties loading failed in AppConfig");
            throw new ConfigException(ioe,"Can't locate file:" +in_filename);