First, to set the record straight, I neither said nor wrote the text
quoted above. I’d never have put it that way. That’s just sloppy
reporting on the part of the publications involved.
The most important thing to understand about class loading and resource
lookup in Java 9 is that, at a fundamental level, they have not changed.
You can search for classes and resources in the same way that you always
have, by invoking Class::forName
and the various getResource*
methods
in the Class
and ClassLoader
classes, regardless of whether your code
is loaded from the class path or the module path. There are still three
built-in class loaders, just as there were in JDK 1.2, and they have the
same delegation relationships. Lots of existing code therefore just
works, out-of-the-box.
There are some nuances, as noted in JEP
261: The concrete type
of the built-in class loaders has changed, and some classes formerly
loaded by the bootstrap class loader are now loaded by the platform class
loader in order to improve security. Existing code which assumes that a
built-in class loader is a URLClassLoader
, or that a class is loaded by
the bootstrap class loader, may therefore require minor adjustments.
A final important difference is that non-class-file resources in a module
are encapsulated by default, and hence cannot be located from outside
the module unless their effective package is
open
.
To load resources from your own module it’s best to use the
resource-lookup methods in Class
or Module
, which can locate any
resource in your module, rather than those in ClassLoader
, which can
only locate non-class-file resources in the open
packages of a module.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…