0

Im using the below code to read a properties file in my servlet. I would like to get clarified about the getClass.getClassLoader().getResourceAsStream(...) line.

From my understanding, getClassLoader() will get the class loader associated with the Servlet class that I have defined. But how that is associated with propFileName?

Properties prop = new Properties();
String propFileName = "config.properties";
InputStream inputStream = getClass().getClassLoader().getResourceAsStream(propFileName);
prop.load(inputStream);

String user = prop.getProperty("GOOGLE_CLIENT_ID");
String id = prop.getProperty("GOOGLE_REDIRECT_URL");
blalasaadri
  • 5,990
  • 5
  • 38
  • 58
user1050619
  • 19,822
  • 85
  • 237
  • 413
  • 2
    What do you mean by _how that is associated with the propertyfilename_? It's not. It just looks for a resource defined at the path you specify in the classpath. – Boris the Spider Oct 08 '14 at 21:38

1 Answers1

0

public InputStream getResourceAsStream(String name)

According to the Java API, this method finds a resource with a given name/path. The rules for searching resources associated with the ClassLoader (a given class are implemented by the defining class loader of the class).

The parameter "name" must be the "absolute" path for the file, so it will open up the InputStream and pass its instance to the Properties.load(inputStream) method.

This is clearly mentioned in the Java API. Please have a look at : http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#getResourceAsStream(java.lang.String)

Hope this helps!

Nomesh DeSilva
  • 1,649
  • 4
  • 25
  • 43