Originally posted by Peter Chase:
OK, I understand what you are saying now. I didn't know one could do that.
...
P.S. Does the exact code you posted really work? I would have expected the argument to be "java/lang/String.class", not "String.class".
Since I'm not using the ClassLoader's getResource method but that of the class itself, I can use a path relative to where the class file itself is located. Therefore, it finds the class file itself.
Neat huh, using a class file to find itself?
However, your solution is not generally applicable to all ClassLoaders. It only works for ClassLoaders that load classes from a resource whose name is the class name plus ".class". Further, it only works for situations where there is an appropriate type of URL to represent the resource; for Jars, there is one (jar
, but there wouldn't be for all ClassLoaders.
There is no requirement at all for all ClassLoaders to load classes via a resource and a URL. A legal and functional ClassLoader can just override findClass(String). There are perfectly good reasons to do so, and I have written ClassLoaders like this. Your suggestion would not work for such ClassLoaders.
I am well aware of that, I have only tested this with the default class loader and URLClassLoader (and only after I made my original post here).