Task: load any application class using a custom class loader (CustomClassLoader
).
Project with three classes:
Main
- a class containing themain
method, where an instance of theCustomClassLoader
class is created, theCat
class is loaded using it, creating an instance of the classCat
and calling theCat::talk
method;Cat
- loadable class with thetalk
method, which prints the string "Meow" tostdout
;CustomClassLoader
- a class that is an implementation of a custom class loader.
java8 -cp . ru.ispras.j8.manual.Main
or
java8 -jar manual-1.0.jar
Output:
System ClassLoader is ru.ispras.j8.manual.CustomClassLoader@75b84c92
Main Class ClassLoader is ru.ispras.j8.manual.CustomClassLoader@75b84c92
Cat Class ClassLoader is ru.ispras.j8.manual.CustomClassLoader@75b84c92
Meow
- When a
CustomClassLoader
is created, its parent becomessun.misc.Launcher$AppClassLoader
.
CustomClassLoader
gets some system classes to be loaded first, for example classes fromjava.*
packages. If you try to load a class from thejava.*
package, the JVM will throwjava.lang.SecurityException: Prohibited package name: java.*
.