getSystemResourceAsStream() returns null

Posted by Hitesh Solanki on Stack Overflow See other posts from Stack Overflow or by Hitesh Solanki
Published on 2010-03-26T09:44:26Z Indexed on 2010/03/26 9:53 UTC
Read the original article Hit count: 278

Filed under:
|

Hiii... I want to get the content of properties file into InputStream class object using getSystemResourceAsStream(). I have built the sample code. It works well using main() method,but when i deploy the project and run on the server, properties file path cannot obtained ... so inputstream object store null value.

Sample code is here..

public class ReadPropertyFromFile {

    public static Logger logger = Logger.getLogger(ReadPropertyFromFile.class);

    public static String readProperty(String fileName, String propertyName) {
        String value = null;
        try {
            //fileName = "api.properties";
            //propertyName = "api_loginid";

            System.out.println("11111111...In the read proprty file.....");


            //  ClassLoader loader = ClassLoader.getSystemClassLoader();

            InputStream inStream = ClassLoader.getSystemResourceAsStream(fileName);

            System.out.println("In the read proprty file.....");
            System.out.println("File Name :" + fileName);
            System.out.println("instream = "+inStream);

            Properties prop = new Properties();

            try {
                prop.load(inStream);
                value = prop.getProperty(propertyName);
            } catch (Exception e) {
                logger.warn("Error occured while reading property " + propertyName + " = ", e);
                return null;
            }
        } catch (Exception e) {
            System.out.println("Exception = " + e);
        }
        return value;
    }

    public static void main(String args[]) {

      System.out.println("prop value = " + ReadPropertyFromFile.readProperty("api.properties", "api_loginid"));
   }
}

© Stack Overflow or respective owner

Related posts about classloader

Related posts about java