File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes custom class loader in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "custom class loader in java" Watch "custom class loader in java" New topic
Author

custom class loader in java

DeepakN kumar
Greenhorn

Joined: Apr 14, 2012
Posts: 27
HI,
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?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
DeepakN kumar
Greenhorn

Joined: Apr 14, 2012
Posts: 27
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?
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3606
    
  60

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.
Elgs Chen
Greenhorn

Joined: Sep 24, 2012
Posts: 1
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.
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3606
    
  60

Thanks for the clarification, and welcome to the Ranch!
kalai sundar
Ranch Hand

Joined: May 25, 2005
Posts: 69
Check out http://ourownjava.com/how-to-write-a-custom-class-loader-in-java to know more about custom class loader, class loader hierarchy and more.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: custom class loader in java
 
Similar Threads
Plugin autoloading
UserDefine ClassLoader
Problem using custom ClassLoader
Context Class Loader - very very confusing
Please help me understand some concepts about classloader