Home » Java programming language

Java ClassLoader getSystemClassLoader() method with example

ClassLoader Class getSystemClassLoader() method: Here, we are going to learn about the getSystemClassLoader() method of ClassLoader Class with its syntax and example.
Submitted by Preeti Jain, on November 26, 2019

ClassLoader Class getSystemClassLoader() method

  • getSystemClassLoader() method is available in java.lang package.
  • getSystemClassLoader() method is used to find the System class loader for delegation and this will be the default delegation parent for the new instance of the ClassLoader.
  • getSystemClassLoader() method is a static method, it is accessible with the class name and if we try to access the method with the class object then we will not get any error.
  • getSystemClassLoader() method may throw an exception at the time of checking security constraints.
    • SecurityException: In this exception, its checkPermission() method does not allow access to the system classloader when the security manager exists.
    • IllegalStateException: In this exception when called recursively during the construction of the class loader given by the property "java.system.class.loader".

Syntax:

    static ClassLoader getSystemClassLoader();

Parameter(s):

  • It does not accept any parameter.

Return value:

The return type of this method is ClassLoader, it returns the system class loader of ClassLoader type.

Example:

// Java program to demonstrate the example 
// of ClassLoader getSystemClassLoader() method of ClassLoader 

public class GetSystemClassLoader {
    public static void main(String[] args) throws Exception {
        // It returns the Class object attached with the given 
        // classname
        Class cl = Class.forName("GetSystemClassLoader");

        // It returns the ClassLoader object attached with the given 
        // classname
        ClassLoader loader = cl.getClassLoader();

        // Display Loader Class
        System.out.println(loader.getClass());

        // It returns the SystemClassLoader object attached with the 
        // given classname
        loader = loader.getSystemClassLoader();

        // Display SystemClassLoader Class
        System.out.println(loader.getClass());
    }
}

Output

class jdk.internal.loader.ClassLoaders$AppClassLoader
class jdk.internal.loader.ClassLoaders$AppClassLoader


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.