13

I'd like to know the difference between the following two:

MyClass.class.getClassLoader().getResourceAsStream("path/to/my/properties");

and

MyClass.class.getResourceAsStream("path/to/my/properties");

Thank you.

George Sun
  • 881
  • 1
  • 10
  • 20

3 Answers3

14

From the Javadoc for Class.getResourceAsStream():

This method delegates to this object's class loader. Before delegation, an absolute resource name is constructed from the given resource name using this algorithm:

  • If the name begins with a '/' ('\u002f'), then the absolute name of the resource is the portion of the name following the '/'.
  • Otherwise, the absolute name is of the following form: modified_package_name/name
    Where the modified_package_name is the package name of this object with '/' substituted for '.' ('\u002e').

In other words, they do the same thing if the "path" begins with a "/", but if not, then in the latter case, the path will be relative to the class's package, whereas the classloader one will be absolute.

In short, the first fetches path/to/my/properties and the second fetches package/of/myclass/path/to/my/properties.

Mark Peters
  • 80,126
  • 17
  • 159
  • 190
4

From the Class.getClassLoader() documentation:

Returns the class loader for the class. Some implementations may use null to represent the bootstrap class loader. This method will return null in such implementations if this class was loaded by the bootstrap class loader.

So getClassLoader() may return null if the class was loaded by the bootstrap class loader, hence the null check in the Class.getResourceAsStream implementation:

public InputStream getResourceAsStream(String name) {
    name = resolveName(name);
    ClassLoader cl = getClassLoader0();
    if (cl==null) {
        // A system class.
        return ClassLoader.getSystemResourceAsStream(name);
    }
    return cl.getResourceAsStream(name);
}

You'll also note the statement name = resolveName(name); which Mark Peters has explained in his answer.

Community
  • 1
  • 1
Paul Bellora
  • 54,340
  • 18
  • 130
  • 181
2

The main practical difference is that you can use relative paths when you go through the class. So if your properties are in the same package as MyClass, you can do

MyClass.class.getResourceAsStream("properties");
Thilo
  • 257,207
  • 101
  • 511
  • 656