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);
        }
        try
        {
            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);
        }
0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact