Beruflich Dokumente
Kultur Dokumente
risingofsilversurfer.blogspot.com
explain to him about Java class loading. Then I thought "I know
basics about Java Class Loading, but is it complete and accurate?".
So I decided to do some search about the topic and it ended with
writing this post.
mechanism.
1 of 15 30/10/2018, 11:11 PM
¯\_(ツ)_/¯: Class Loading in Java about:reader?url=https://risingofsilversurfer.blogspot.com/2016/10/class-...
Process of finding the byte code for a given class name and then
JVM does not load all classes at the start-up, it only loads the
2 of 15 30/10/2018, 11:11 PM
¯\_(ツ)_/¯: Class Loading in Java about:reader?url=https://risingofsilversurfer.blogspot.com/2016/10/class-...
needed.
The same class loaded by two separate class loaders are not
equal. Therefore you cannot cast one of those class to other class,
although those were loaded from same class byte code. A loaded
class is internally identified by [the package name, class name, the
Field Data
Method Data
Java has following class loader types in addition to user built class
loaders.
3 of 15 30/10/2018, 11:11 PM
¯\_(ツ)_/¯: Class Loading in Java about:reader?url=https://risingofsilversurfer.blogspot.com/2016/10/class-...
code.
So when you execute a java program with -verbose:class JVM
argument, You can see that first few lines in the output that starts
with "[Loaded" are related to the class loading done by Bootstrap
Class Loader from jre\lib\rt.jar file and other important jar files the
jre\lib directory in your Java installation. This location is known as
4 of 15 30/10/2018, 11:11 PM
¯\_(ツ)_/¯: Class Loading in Java about:reader?url=https://risingofsilversurfer.blogspot.com/2016/10/class-...
Loader. This class loader loads the classes from classes and jar
5 of 15 30/10/2018, 11:11 PM
¯\_(ツ)_/¯: Class Loading in Java about:reader?url=https://risingofsilversurfer.blogspot.com/2016/10/class-...
classpath.
You can use -cp or -classpath command line option to define the
classpath, this will override the value defined in CLASSPATH
environment variable.
When you use -jar command line option, it will ignore the values in
-cp and -classpath command line options, and also the value of
CLASSPATH environment variable. It will only consider the Class-
If two classes with the same name exists in the classpaths, the
of sun.misc.Launcher$AppClassLoader, by using
-Djava.system.class.loader command line option.
6 of 15 30/10/2018, 11:11 PM
¯\_(ツ)_/¯: Class Loading in Java about:reader?url=https://risingofsilversurfer.blogspot.com/2016/10/class-...
etc.
7 of 15 30/10/2018, 11:11 PM
¯\_(ツ)_/¯: Class Loading in Java about:reader?url=https://risingofsilversurfer.blogspot.com/2016/10/class-...
Class loaders (at least JVM class loaders) follow following algorithm
when it wants to load a class. (This algorithm is based on
2. If the class was not found in the cache, ask (delegate) the parent
class loader to load the class. (Please remember that Bootstrap
3. Parent class loader also does above steps recursively until the
class found in its cache or no parent class loader found. (Ask
parent's parent class loader to load, if parent class loader did not
find the class in its cache.).
4. If the parent class loader can find and load the class, it store it in its
5. If parent class loader cannot load the class, the class loader tries to
6. If the class loader cannot find the class in its local cache and cannot
get the class from parent class loaders and cannot load the class
8 of 15 30/10/2018, 11:11 PM
¯\_(ツ)_/¯: Class Loading in Java about:reader?url=https://risingofsilversurfer.blogspot.com/2016/10/class-...
2. It does not find it in local cache, so it ask its parent class loader
(which is Extensions Class Loader) to load the
javax.imageio.ImageIO class.
our example, Bootstrap class loader should not find the class in its
7. If Bootstrap Class Loader did not find it in its local cache, and
because it does not have any parent class loader, it tries to load the
9 of 15 30/10/2018, 11:11 PM
¯\_(ツ)_/¯: Class Loading in Java about:reader?url=https://risingofsilversurfer.blogspot.com/2016/10/class-...
sources.
class in its local cache. Then it returns the class to its child class
loader which is System Classpath Class Loader.
loader's cache. The class loader will try to load the class itself, only
to the children class loaders of that parent class loader. But the
classes loaded by a child class loader are not visible to any of
10 of 15 30/10/2018, 11:11 PM
¯\_(ツ)_/¯: Class Loading in Java about:reader?url=https://risingofsilversurfer.blogspot.com/2016/10/class-...
There are 3 main stages in loading a class from byte code to proper
1. Loading - Class Loader search for the class file (byte code) in the
structure). This step creates a basic structure for the class, but it
does not have all the important data filled yet.
2. Linking - This step tries to convert byte code data to proper runtime
information. This step is sub divided into 3 more sub steps.
1. Byte Code Verification - the byte code is checked for the correct
format and other checks. This step ensures that loaded class is
structurally correct.
11 of 15 30/10/2018, 11:11 PM
¯\_(ツ)_/¯: Class Loading in Java about:reader?url=https://risingofsilversurfer.blogspot.com/2016/10/class-...
default values.
installed in your machine, JRE loads the classes from system jar
files into internal representations and then dump those loaded
If this feature is enabled, you will see many lines with "[Loaded
12 of 15 30/10/2018, 11:11 PM
¯\_(ツ)_/¯: Class Loading in Java about:reader?url=https://risingofsilversurfer.blogspot.com/2016/10/class-...
file.
ClassNotFoundException
dynamically tries to load the class at runtime and the class loader
did not find the requested class. Main reason is, the class is not
java program tries to load a class dynamically, using its string name.
Due to this reason, the compiler does not detect the unavailability of
13 of 15 30/10/2018, 11:11 PM
¯\_(ツ)_/¯: Class Loading in Java about:reader?url=https://risingofsilversurfer.blogspot.com/2016/10/class-...
NoClassDefFoundError
References:
http://docs.oracle.com/javase/7/docs/technotes/guides/security
/spec/security-spec.doc5.html
http://docs.oracle.com/javase/7/docs/technotes/tools/
findingclasses.html
http://docs.oracle.com/javase/tutorial/ext/basics/install.html
http://javapapers.com/core-java/java-jvm-run-time-data-areas/
http://www.artima.com/insidejvm/ed2/jvm2.html
http://what-when-how.com/enterprise-javabeans-3/exploring-class-
loading-ejb-3/
http://blog.jamesdbloom.com/JVMInternals.html#method_area
https://docs.oracle.com/javase/tutorial/essential/environment
14 of 15 30/10/2018, 11:11 PM
¯\_(ツ)_/¯: Class Loading in Java about:reader?url=https://risingofsilversurfer.blogspot.com/2016/10/class-...
/paths.html
http://docs.oracle.com/javase/1.5.0/docs/tooldocs/windows
/classpath.html
http://docs.oracle.com/javase/7/docs/technotes/guides/extensions
/index.html
http://docs.oracle.com/javase/tutorial/ext/
https://blogs.oracle.com/sundararajan/entry
/understanding_java_class_loading
https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-5.html
http://www.techjava.de/topics/2008/01/java-class-loading/
http://docs.oracle.com/cd/E11035_01/wls100/programming
/classloading.html
15 of 15 30/10/2018, 11:11 PM