aspose file tools*
The moose likes Java in General and the fly likes Please help me understand some concepts about classloader Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Please help me understand some concepts about classloader" Watch "Please help me understand some concepts about classloader" New topic
Author

Please help me understand some concepts about classloader

gg cowra
Greenhorn

Joined: Oct 03, 2008
Posts: 3
I am really confused by the classloader hierarchy. for example, a very simple AAA.java file:


When I run the AAA, it is absolutely loaded by system classloader, but the String.class, System.class and Integer.class referenced by it should be loaded by bootstrap classloader. So, class AAA and those it referenced are loaded by different classloaders. why AAA can access them ?


who am i
Ramon Anger
Ranch Hand

Joined: Apr 19, 2011
Posts: 56

Hi,

this works because classloaders inside a JVM build a hierarchy.

See http://www.objectsource.com/j2eechapters/Ch21-ClassLoaders_and_J2EE.htm or http://www.digilife.be/quickreferences/PT/Understanding%20the%20Java%20ClassLoader.pdf (especially the "further reading" section at page 17)

Regards,
Ramon


Blackbelt on BlackBeltFactory.com.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Classloader hierarchies aren't "Beginning Java". Moving to Java in General.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
gg cowra
Greenhorn

Joined: Oct 03, 2008
Posts: 3
Ramon Anger wrote:Hi,

this works because classloaders inside a JVM build a hierarchy.

See http://www.objectsource.com/j2eechapters/Ch21-ClassLoaders_and_J2EE.htm or http://www.digilife.be/quickreferences/PT/Understanding%20the%20Java%20ClassLoader.pdf (especially the "further reading" section at page 17)

Regards,
Ramon


From the hyperlinks you provided, I got some valuable knowledge that I had never known.

Sing highly praise for your great help!
Prakash Rai
Ranch Hand

Joined: Jan 10, 2011
Posts: 106

Boot Strap class loader or default class loader which is defined in side JVM By default Boot Strap class loader will load your class. But boot strap class loader will load class from local file system.Also you can define you own class loader.
please correct me if I am wrong.
gg cowra
Greenhorn

Joined: Oct 03, 2008
Posts: 3
Somprakash Rai wrote:Boot Strap class loader or default class loader which is defined in side JVM By default Boot Strap class loader will load your class. But boot strap class loader will load class from local file system.Also you can define you own class loader.
please correct me if I am wrong.


According to the delegation principle of Java classloaders' hierarchy, the bootstrap classloader will be delegated to try to load my classes, but it could not find them, because it only loads the classes in the JRE/lib or as specified by the command line switch -Xbootclasspath . By default my classes will be eventually loaded by System-Classpath classloader or some children of it.

The key of my problem is the visibility principle of classloader operation. The fact that classes written by myself can access standard JDK classes just because the bootstrap classloader is a parent classloader above the system-classpath classloader.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19785
    
  20

That's something you can't prevent. When a ClassLoader tries to load a class it first asks its parent if the class has already been loaded. If so it will return the class as loaded by the parent. Only if the parent (or the parent's parent, etc) hasn't loaded the class yet will the ClassLoader try to load the class itself. Since the system class loader will always be somewhere at the top of the ClassLoader hierarchy, all classes from the core API are always available. The same goes for classes on the class path.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Please help me understand some concepts about classloader