Sunday, February 23, 2014

CLASSPATH and Class Loading

It is used by JVM to locate the java class files. Locations are separated by colons (UNIX) or semicolons (Windows) in CLASSPATH.  Class files are group into JAR which is a zip file.

When JVM starts, it loads the bootstrapping classes into memory.  The classes in the CLASSPATH is loaded when they are referenced.  Variable Xbootclasspth allows specific bootstrap classes to be loaded before the Java core classes.

Classes are accessed on the last declared-last load basis.  For classes with same name in different JARs, only the last declared one is considered valid.  Exception to this is classloader.  JVM supports mulitple classloaders with same name.  By default, a JVM has 2 classloaders - one for the bootstrap classes and one for those in the CLASSPATH.  Customed classloaders can be used to load classes with same name.  The application must be designed from onset for this and implemented with Java API.  Java EE application servers are example of this which provide an isolated environment for each application.

Each JAR has no awareness of external dependency to other AR.  Unless the JVM is restarted, JAR cannot be upgraded.

java.lang.IncompatibleClassChangeError is thrown when there is a class conflict.  This type of errors can be common for enterprise application with multiple integrated parts.

No comments: