Java ClassLoader getSystemClassLoader()方法与示例

ClassLoader类getSystemClassLoader()方法 (ClassLoader Class getSystemClassLoader() method)

  • getSystemClassLoader() method is available in java.lang package.

    getSystemClassLoader()方法在java.lang包中可用。

  • 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()方法用于查找要委派的系统类加载器,这将是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()方法是一个静态方法,可以使用类名进行访问,如果尝试使用类对象访问该方法,则不会出现任何错误。

  • getSystemClassLoader() method may throw an exception at the time of checking security constraints.

    在检查安全性约束时, getSystemClassLoader()方法可能会引发异常。

    • SecurityException: In this exception, its SecurityException :在此异常中,当安全管理器存在时,其checkPermission() method does not allow access to the system classloader when the security manager exists.checkPermission()方法不允许访问系统类加载器。
    • IllegalStateException: In this exception when called recursively during the construction of the class loader given by the property IllegalStateException :在此异常中,当在构造由属性"java.system.class.loader".“ 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.

该方法的返回类型为ClassLoader ,它返回ClassLoader类型的系统类加载器 。

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


翻译自: https://www.includehelp.com/java/classloader-getsystemclassloader-method-with-example.aspx

你可能感兴趣的:(java,python,jvm,tomcat,spring)