getClassLoader().getResourceAsStream && .getResourceAsStream()


General J2EE: getClassLoader().getResourceAsStream && .getResourceAsStream()

  1. Hi,

    We can laod a particular resource(properties file, config files etc) as InputStream in two ways...
    Suppose TestClassLoader is our class where we load our resource...

    1.) TestClassLoader.class.getResourceAsStream("com/somepacakage/");

    2.) TestClassLoader.class.getClassLoader().getResourceAsStream("com/somepacakage/");

    -- Whats is the difference between above two approaches?
    -- Which should be used when?

    Jay Khimani
  2. I don't think there any different. If you look at the implementation of Class.getResourceAsStream(), it delegates this call to the underlying class loader.
  3. subtle differences[ Go to top ]

    Javaworld has a decent article on this subject which points out a subtle difference in how resource should be referenced in the classpath/package structure.

    according to this article it is important to note the leading slash when calling getResourceAsStream() on the Class class.

    Class.getResourceAsStream ("/some/pkg/");