gpt4 book ai didi

java - 是否可以创建一个空的 ClassLoader?

转载 作者:行者123 更新时间:2023-11-30 07:11:50 30 4
gpt4 key购买 nike

我想创建一个新的 ClassLoader 实例,它在开始时绝对不包含任何类。它不应该有一个可用的类。甚至没有 java.lang 类。我希望能够为我自己的位置手动加载每个类。 (使用覆盖的 defineClass 方法)

问题是我对此所做的每一次尝试都失败了。任何新创建的 ClassLoader 都包含 CLASSPATH 变量中的所有类。

现在是我的问题:
是否可以创建一个空的 ClassLoader? (不应加载任何类 - 特别是来自 CLASSPATH 的类)

提前致谢,丹尼斯

最佳答案

我认为这是不可能的。

当您定义一个类加载器(自行创建)时,您需要定义它的父类加载器。当您不定义时,引导类加载器是默认的。

父类加载器的需要是一个安全问题。 java.lang 来自bootstrap 类加载器。


这里有来自 http://en.wikipedia.org/wiki/Java_Classloader 的更深入的解释:

Each Java class must be loaded by a class loader.[2] Furthermore, Java programs may make use of external libraries (that is, libraries written and provided by someone other than the author of the program) or they may be composed, at least in part, of a number of libraries.

When the JVM is started, three class loaders are used:[3][4]

Bootstrap class loader

Extensions class loader

System class loader

The bootstrap class loader loads the core Java libraries[5] located in the /jre/lib directory. This class loader, which is part of the core JVM, is written in native code.

The extensions class loader loads the code in the extensions directories (/jre/lib/ext,[6] or any other directory specified by the java.ext.dirs system property). It is implemented by the sun.misc.Launcher$ExtClassLoader class.

The system class loader loads code found on java.class.path, which maps to the CLASSPATH environment variable. This is implemented by the sun.misc.Launcher$AppClassLoader class.

关于java - 是否可以创建一个空的 ClassLoader?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20784062/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com