Today, I was surprised (likely due to inexperience) to find out that you can, and it is actually useful to, pass an empty path (literally an empty string) to ClassLoader.getResources, i.e.
ClassLoader.getSystemClassLoader().getResources(""). This, from some testing, returns one or two directories of where my application .class files live (and does not include the directories of 3rd-party packages). (Example usage: Get all of the Classes in the Classpath.)
Presumably, this is because the Java System ClassLoader is one of the three ClassLoaders that loads my own application classes (c.f. http://www.oracle.com/technetwork/articles/javase/classloaders-140370.html), so it's not surprising that URL returned points to the directory of my application class files.
But why, and how, does the empty string achieve this? I did not find it documented. Is this empty path derivative of more common Java convention? It's certainly not Linux - you can't cd into an empty path in bash. I'd appreciate it if someone can help me understand this.
In another note, I noticed that getResources(".") achieves the same thing.
Additions for comment discussion
public class myTest {
public static void main(String[] args) throws Exception {
ClassLoader classLoader = ClassLoader.getSystemClassLoader();
URL[] urls = ((URLClassLoader) classLoader).getURLs();
for (int n = 0; n < urls.length; n++)
System.out.println(urls[n]); //lists external.jar
Enumeration<URL> roots = classLoader.getResources(".");
while (roots.hasMoreElements()) {
URL url = roots.nextElement();
System.out.println("getResources: " + url); //does not list external.jar
}
}
}
Command to execute: java -cp ".:external.jar" myTest