Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

custom class loader in java

 
DeepakN kumar
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
DeepakN kumar
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the clarification, and welcome to the Ranch!
 
kalai sundar
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic