Take the tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am just now beginning to learn the internal architecture of Java. I have roughly understood the concept of class loading which loads the required classes when jvm runs, ClassNotFoundException is thrown when a class is not found and specific class loader loads the classes referenced by the class.

Can someone please explain clearly the flow of class loading i.e. the sequence of bootstrap class loading and user-defined class loading in the sample Java code below.

import java.io.File;
public class Sample
{
    public static void main(String[] args)
    {
        String fileName = "sample";
        File file = new File(fileName);
        file.isFile();
    }
} 

Also I learnt from a reference material that "classloader maintains the namespaces of the classes it loads". By namespaces, does that mean the literal names of the class? Also can someone please explain the implication/advantage of that?

share|improve this question
6  
A good resource to get started zeroturnaround.com/rebellabs/… –  Narendra Pathai 15 hours ago
 
add comment

5 Answers

up vote 16 down vote accepted

You will run your Sample class as follows

> java Sample

for little magic, check out the output of-verbose:class option and you see tons of following lines..

[Opened C:\jdk1.6.0_14\jre\lib\rt.jar]
[Loaded java.lang.Object from C:\jdk1.6.0_14\jre\lib\rt.jar]
[Loaded java.io.Serializable from C:\jdk1.6.0_14\jre\lib\rt.jar]
[Loaded java.lang.Comparable from C:\jdk1.6.0_14\jre\lib\rt.jar]
.
.
.
.
.
.
[Loaded java.security.cert.Certificate from C:\jdk1.6.0_14\jre\lib\rt.jar]
[Loaded Sample from file:/D:/tmp/]
[Loaded java.lang.Shutdown from C:\jdk1.6.0_14\jre\lib\rt.jar]
[Loaded java.lang.Shutdown$Lock from C:\jdk1.6.0_14\jre\lib\rt.jar]

You see a bunch of classes from \jre\lib\rt.jar are loaded, much before your class is loaded by Bootstrap class loader (or Primordial ). These are the pre-requisite for running any Java program hence loaded by Bootstrap.

Another set of jars is loaded by Extension class loader. In this particular example, there was no need of any classes from the lib \jre\lib\ext hence its not loaded. But Extension class loader are specifically assigned the task of loading the classes from the extension lib.

EDIT: Apart from the standard platform java classes Sun/Oracle also provide a set of jars which are used to extend the platform's core API. The jars placed in the extension lib folder are automatically placed in the classpath and hence not needed to be included in classpath explicitly. Here is nice official article on the same topic.

Finally, your class Sample is loaded by Application class loader after Bootstrap and Extension have finished loading.

share|improve this answer
 
@Santhosh: thanks..Ur answer suffices but can u still more elaborate on Extension class loader? Its utility is unclear –  aarish 14 hours ago
 
@aarish I have edited my post. Please check. –  Santosh 14 hours ago
 
@santosh +1 for mentioning the -verbose:class option. –  Geek 9 hours ago
add comment

JVM maintains a runtime pool is permgen area where classes are loaded. Whenever a class is referenced defualt class loader finds the class is the class path and loads it into this pool. And this is not specific to user defined classes or classes provided in JDK. When a class id referenced it is loaded into the memory.

Classes loaded by the ClassLoader is stored internally in the ClassLoader instance

// The classes loaded by this class loader. The only purpose of this table
// is to keep the classes from being GC'ed until the loader is GC'ed.
private final Vector<Class<?>> classes = new Vector<>();

When class needs to be added to the memory follwing function is called

// Invoked by the VM to record every loaded class with this loader.
void addClass(Class c) {
    classes.addElement(c);
}

Found an useful diagram on how Class Loaders work.

enter image description here

share|improve this answer
 
you mean to say that - " as long as its class loader exists, the class exists"?? –  TheLostMind 15 hours ago
1  
Yes and ClassLoader instance does not get GCed as it is referenced by JVM thread. Infact that is why even if you have a Singleton class it is possible to create two instances with two different class loaders. –  Aniket Thakur 15 hours ago
 
So.. you mean to say "if I have several hundred classes being loaded by the same classloader, then until the class loader is GCed, the classes will not be GCed"??... How is this efficient? –  TheLostMind 13 hours ago
1  
No ClassLoader instances are same as any other Objects in the heap. The statement that it does not get GCed come from the fact that ClassLoaders have references from JVM threads which run till the java process is completed and JVM shuts down. For eg the BootStrap Class Loader is a native implementation meaning its code is embedded in JVM. So it's reference will always be alive. Hence we say they are not the potential candidates for GC. Other than that GC treats them the same way. –  Aniket Thakur 12 hours ago
1  
@TheLostMind I can't say for sure, but the code Aniket posted in the answer implies that the GC treats them no differently; otherwise the classloader wouldn't need to keep a Vector around just to prevent garbage collection. –  Chris Hayes 12 hours ago
show 4 more comments

Classloader hierarchy

Whenever a new JVM is started the bootstrap classloader is responsible to load key Java classes (from java.lang package) and other runtime classes to the memory first. The bootstrap classloader is a parent of all other classloaders. Consequently, it is the only one without a parent.

Next comes the extension classloader. It has the bootstrap classloader as parent and is responsible for loading classes from all .jar files kept in the java.ext.dirs path–these are available regardless of the JVM’s classpath.

The third and most important classloader from a developer’s perspective is the system classpath classloader, which is an immediate child of the extension classloader. It loads classes from directories and jar files specified by the CLASSPATH environment variable, java.class.path system property or -classpath command line option.

Classloader hierarchy

ClassLoader Namespace

In Java a class is uniquely identified using ClassLoader + Class as the same class may be loaded by two different class loaders.

Class A loaded by ClassLoader A != Class A loaded by ClassLoader B

How is it helpful?

It is helpful for defining different protection and access policies for different classloaders. Take an example of applet which is loaded using a different classloader, you would not want a third party application all access to your resources. So for security its important to maintain different namespaces.

share|improve this answer
1  
this clarifies stuffs to me to a certain extent. can u please answer to my question relating to class loader's name-spaces? –  aarish 15 hours ago
1  
I have added the explanation. Hope it helps. –  Narendra Pathai 14 hours ago
add comment

The Java Virtual Machine starts up by creating an initial class, which is specified in an implementation-dependent manner, using the bootstrap class loader (§5.3.1). The Java Virtual Machine then links the initial class, initializes it, and invokes the public class method void main(String[]). The invocation of this method drives all further execution. Execution of the Java Virtual Machine instructions constituting the main method may cause linking (and consequently creation) of additional classes and interfaces, as well as invocation of additional methods.

read this link

share|improve this answer
add comment

Java Classloader:The Java Classloader is a part of the JRE that dynamically loads Java classes into the JVM. Usually classes are only loaded on demand. The Java run time system does not need to know about files and file systems because of classloaders.

Each Java class must be loaded by a class loader

When the JVM is started, three class loaders are used:

(1) Bootstrap class loader:It loads the core Java libraries located in the <JAVA_HOME>/jre/lib directory. Bootstrap class loade part of the core JVM and written in native code.

(2) Extensions class loader:It loads the code in the extensions directories (<JAVA_HOME>/jre/lib/ext, or any other directory specified by the java.ext.dirs system property)

(3)System class loader:It loads code found on java.class.path, which maps to the CLASSPATH environment variable.

Principles :Java ClassLoader works in three principles delegation, visibility and uniqueness. Delegation is an important concept to understand when learning about classloaders.

Read more: http://javarevisited.blogspot.com/2012/12/how-classloader-works-in-java.html#ixzz2oei79ovs

Flow of Classloader : enter image description here

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.