10

I google how below code loads the resource Abc.class.getClassLoader().getResourceAsStream("abc.txt")
and find that it searchs the resource in all jar file and zip file in class path.

But when i tried it I am not able to loads it but if i give package path then I am able to loads it can someone tell me how getResourceAsStream search the class path

Thanks

one scenario is :- My below code is a simple program and my resource file abc.txt is inside com.abc package. when i specify path of package it worked and when i did not it does not work.

package com.abc;

public class ResourceExp {

    public static void main(String args[])
    {
        new ResourceExp().getResource();
    }

    public void getResource()
    {
        String name = "abc.txt";
        // worked
        System.out.println(ResourceExp.class.getClassLoader().getResourceAsStream("com/abc/"+name));
        //not workded
        //System.out.println(ResourceExp.class.getClassLoader().getResourceAsStream(name));

    }

}    

if getResourceAsStream looks the resource in all jar file and directory then why i have to specify the package path

Puce
  • 37,247
  • 13
  • 80
  • 152
yoga
  • 133
  • 1
  • 2
  • 8

3 Answers3

17

I google how below code loads the resource Abc.class.getClassLoader().getResourceAsStream("abc.txt") and find that it searchs the resource in all jar file and zip file in class path.

Thats correct when you work only with a single ClassLoader (most non-OSGi/ non-modular environments). Then all content of all JARs can be seen as one big tree, where classes and resources of JARs, which occur prior in the class path, win over those of JARS, which occur further behind.

But when i tried it I am not able to loads it but if i give package path then I am able to loads it can someone tell me how getResourceAsStream search the class path

Abc.class.getClassLoader().getResourceAsStream("abc.txt")

searches at root of the tree while:

Abc.class.getResourceAsStream("abc.txt")

searches relative to the package of Abc.

Abc.class.getResourceAsStream("/abc.txt")

searches at the root of the tree again.

All these methode will only search in the specified directory (or the root directory) and won't traverse and search the whole tree.

Personally, I usually always use the latter two versions (Class.getResourceAsStream) and rarely use the ClassLoader.getResourceAsStream method directly.

Puce
  • 37,247
  • 13
  • 80
  • 152
  • 1. what is the meaning of 'search root of the tree' i.e all jar inside the classpath. if not how i search all jar file. – yoga Nov 26 '14 at 18:56
  • and what is the meaning of (most non-OSGi/ non-modular environments) – yoga Nov 26 '14 at 18:57
  • 1. It will search in all jars which provide the specified directory. If you're looking for a file in the root directory, then it will search in the root directory (but only the root directory) of all JARs. – Puce Nov 26 '14 at 21:58
  • Module systems such as OSGi or the NetBeans Module System provide a ClassLoader for each bundle/ module (i.e. JAR). – Puce Nov 26 '14 at 22:00
2

For example, you can make a "resources" source folder, put files in it, then use Thread.currentThread().getContextClassLoader().getResourceAsStream("abc.txt");
I always use this method.

1

I used this:

InputStream file = YOURCLASSNAME.class.getClassLoader().getResourceAsStream("app.properties");

If I am using this, it doesn't show file not found and read the exact file with our any path, but you should put your app.properties file into main/resources inside in the project package folder.

סטנלי גרונן
  • 2,917
  • 23
  • 46
  • 68
Karthi C
  • 31
  • 6