Task: load any application class using a custom class loader (CustomClassLoader).
Project with three classes:
Main- a class containing themainmethod, where an instance of theCustomClassLoaderclass is created, theCatclass is loaded using it, creating an instance of the classCatand calling theCat::talkmethod;Cat- loadable class with thetalkmethod, which prints the string "Meow" tostdout;CustomClassLoader- a class that is an implementation of a custom class loader.
java8 -cp . ru.ispras.j8.manual.Mainor
java8 -jar manual-1.0.jarOutput:
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
CustomClassLoaderis created, its parent becomessun.misc.Launcher$AppClassLoader.
CustomClassLoadergets 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.*.
