I am facing an issue where the class loader is unable to load an xml file that is in the classpath.
Below code first prints the files that are present in the classpath. Next it is supposed to print the location of the specific xml file that I want to load (hbase-site.xml, which is present in the class path).
System.out.println("Displaying Class Loader URLs:-");
URLClassLoader ucl = ((URLClassLoader)Thread.currentThread().getContextClassLoader());
for (URL url : ucl.getURLs()) {
System.out.println(url);
}
System.out.println("Reading hbase-site.xml from: " + ucl.getResource("hbase-site.xml"));
I am getting the output as below
Displaying Class Loader URLs:-
file:/D:/hbase-master/hbase-master/bin/
file:/D:/hbase-master/hbase-master/conf/hbase-site.xml
file:/C:/Program%20Files/Java/jdk1.8.0_51/lib/tools.jar
file:/D:/hbase-master/hbase-master/hbase-server/target/
Reading hbase-site.xml from: null
I can see that hbase-site.xml is getting output in the list of Class loader URLs.
But the getResource() method is not able to locate the hbase-site.xml file - it returns null. I was expecting it to output 'file:/D:/hbase-master/hbase-master/conf/hbase-site.xml'. I am not able to understand this. Kindly help.
PS:
- For reasons that I do not understand, System.getenv("java.class.path") returns null even though the classpath was set when calling the program.
- The file names look a bit odd, since I am using Cygwin on Windows. However I have verified that this is not an issue. I was able to load a file that was present in a jar file (the jar file was in classpath).