Class.getResource():
1. Resolves name you pass to it. If the name has a starting "/", then it takes it relative from the classpath. means.. "/config/x.txt" == in classpath, config/x.txt should be there
2. If you dont pass a starting "/" then it appends the package name to the file...so, if the name is "config/x.txt" and the class is com.xxx.MyClass, it will look for com/xxx/config/x.txt.
ClassLoader.getResource():
Doesnt do any name resolving. Delegates to parent classloader, if not found, looks in current classloader.
This is my execution:
C:\workspace\CL\bin>java -cp .;CL-1.0.jar a.B
I have a config file called POS.properties in ./config folder.
I tried to read POS.properties in 3 ways.
(i) From main method,
this.class.getResource(/config/POS.properties);
Will find it. From current path, it exists in config/
this.class.getResource(config/POS.properties);
Cant find it. From current path, Looks for in a/config/POS.properties.
this.class.getClassLoader().getResource(/config/POS.properties);
Cant find it.
this.class.getClassLoader().getResource(config/POS.properties);
Will find it.
No comments:
Post a Comment