Can some one tell me whether it is possible to write a custom classloader that can load a java system class say java.util* package?
I understand that by overriding the "loadclass" method of classloader, the custom classloader can change the deligation.Also "findClass" can be overridden to search for the class file. SO in this way is it not possible to implement custom class by changing the delegation model and always load the system classes by custom class loader?
I believe you can change the system class loader by setting the value of the java.system.class.loader system property from the command line (-Djava.system.class.loader=com.coderanch.MyClassLoader).
However, I would like to point out that it is illegal not allowed to modify any of the core API classes. From http://www.oracle.com/technetwork/java/javase/terms/license/index.html:
F. JAVA TECHNOLOGY RESTRICTIONS. You may not create, modify, or change the behavior of, or authorize your licensees to create, modify, or change the behavior of, classes, interfaces, or subpackages that are in any way identified as "java", "javax", "sun", “oracle” or similar convention as specified by Oracle in any naming convention designation.
Of course that would be a huge security risk. I'm no expert on Java security, but I believe that in a secured environment the Security Manager wouldn't let you use your own class loader, for precisely this reason.
DeepakN kumar wrote:Thanks. So it is technically possible to change the delegation order let the custom class loader load the java classes from packages java* & javax* etc right? So isn't it a security threat to java?
No, it's technically not possible to override the packages starting with java and javax. Let's see the code in the preDefineClass in java.lang.ClassLoader
This method is called by defineClass, and all defineClass's are marked as final. Therefore, it's not possible for subclasses of ClassLoader to override this logic.