3 Java class loading interview Q&As

Q1. What do you know about Java class loading? Explain Java class loaders?
A1. Class loaders are hierarchical. Classes are introduced into the JVM as they are referenced by name in a class that is already running in the JVM. So, how is the very first class loaded? The very first class is specially loaded with the help of static main( ) method declared in your class. All the subsequently loaded classes are loaded by the classes, which are already loaded and running. A class loader creates a namespace. All JVMs include at least one class loader that is embedded within the JVM called the primordial (or bootstrap) class loader. The JVM has hooks in it to allow user defined class loaders to be used in place of primordial class loader. Let us look at the class loaders created by the JVM.

Java Class Loader Basics

Java Class Loader Basics

Class loaders are hierarchical and use a delegation model when loading a class. Class loaders request their parent to load the class first before attempting to load it themselves. When a class loader loads a class, the child class loaders in the hierarchy will never reload the class again. Hence uniqueness is maintained. Children class loaders are visible to classes loaded by its parent class loaders. For instance, classes loaded by the system class loader have visibility into classes loaded by the extension and Bootstrap class loaders but not vice-versa.

Q2. Explain static vs. dynamic class loading?
A2. Classes are statically loaded with Java’s “new” operator.

Dynamic loading is a technique for programmatically invoking the functions of a class loader at run time. Let us look at how to load classes dynamically.

The above static method returns the class object associated with the class name. The string className can be supplied dynamically at run time. Unlike the static loading, the dynamic loading will decide whether to load the class Car or the class Jeep at runtime based on a properties file and/or other runtime conditions. Once the class is dynamically loaded the following method returns an instance of the loaded class. It’s just like creating a class object with no arguments.

Static class loading throws “NoClassDefFoundError” if the class is not found and the dynamic class loading throws “ClassNotFoundException” if the class is not found.

Q3. What tips would you give to someone who is experiencing a class loading or “Class Not Found” exception?
A3. “ClassNotFoundException” could be quite tricky to troubleshoot. When you get a ClassNotFoundException, it means the JVM has traversed the entire classpath and not found the class you’ve attempted to reference.

1) Stand alone Java applications use -cp or -classpath to define all the folders and jar files to look for. In windows separated by “;” and in Unix separated by “:”.

2) Determine the jar file that should contain the class file within the classpath — war/ear archives and application server lib directories. Search recursively for the class.

You can also search for the class at www.jarfinder.com

3) Check the version of the jar in the manifest file MANIFEST.MF, access rights (e.g. read-only) of the jar file, presence of multiple versions of the same jar file and any jar corruption by trying to unjar it with “jar -xvf …”. If the class is dynamically loaded with Class.forName(“com.myapp.Util”), check if you have spelled the class name correctly.

4) Check if the application is running under the right JDK? Check the JAVA_HOME environment property

5) -verbose:class option in your JVM. With the -verbose option all the classes that are loaded are listed, along with the JAR file or directory from which they were loaded. The “class” output shows additional information, such as when superclasses are being loaded, and when static initializers are being run.

6) Creating a Java dump and analyzing the Java dump for class loading issues. The Java dumps are created under following circumstances.

— When a fatal native JVM error.
— When the JVM runs out of heaps memory space.
— When a signal is sent to the JVM (e.g. Control-Break is pressed on Windows, Control-\ on Linux, or kill -3 on Unix)

There are tools like jstack, jmap, hprof, and Eclipse Memory Analyzer (MAT) to analyze the Java dumps.

7) Some of the libraries provide API to list the version number. For example, The Eclipse link MOXy library provides a method as shown below.

8) The org.jboss.test.util.Debug class has a method displayClassInfo(Class clazz, StringBuffer results) to display the loaded class details. This is done programmatically. What this class essentially does is

9) The http://www.findjar.com is an onlime search engine that can list possible jar files in which a particular class file like java.sql.Connection can be found.

10) You can print the jar files that are used at runtime by adding the following code snippet.


300+ Java Interview FAQs

Tutorials on Java & Big Data