Java getClass().getClassLoader() 为空,为什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1921238/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
getClass().getClassLoader() is null, why?
提问by jeff porter
I've got some code that calls..
我有一些代码可以调用..
x = getClass().getClassLoader();
This returns null though.
虽然这会返回 null。
When I start the same code not from Eclipse, but the command line, it returns a classloader.
当我不是从 Eclipse 而是从命令行启动相同的代码时,它返回一个类加载器。
I can hack the code to do this...
我可以破解代码来做到这一点......
if (getClass().getClassLoader() == null)
{
x = ClassLoader.getSystemClassLoader().getSystemResourceAsStream( loadedPropFileName );
}
both are compiled and run with the same JVM. (I'm 99.99% sure).
两者都使用相同的 JVM 编译和运行。(我有 99.99% 的把握)。
Anyone have any ideas why the first would return null for the classloader?
任何人都知道为什么第一个会为类加载器返回 null ?
Edit:
编辑:
My question is does "Anyone have any ideas why the same class would return null when started via Eclipse and a class loader when loaded from the command line."
我的问题是“任何人都知道为什么同一个类在通过 Eclipse 启动时会返回 null,而从命令行加载时会返回一个类加载器。”
Thanks for the advice that the Bootstap loader must be loading the class in Eclipse. I've no idea why this happens though.
感谢您提出 Bootstap 加载器必须在 Eclipse 中加载类的建议。我不知道为什么会发生这种情况。
采纳答案by Michael Borgwardt
回答by Itay Maman
"This method will return null in such implementations if this class was loaded by the bootstrap class loader." http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Class.html#getClassLoader()
“如果此类由引导类加载器加载,则此方法将在此类实现中返回 null。” http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Class.html#getClassLoader()
回答by PSpeed
One thing is certain, Eclipse has a deeper and more complicated classloader setup than when you are running from the command line. If you are seeing differences in how a class's classloader appears in one versus the other then that is a pretty likely reason.
有一点是肯定的,与从命令行运行相比,Eclipse 具有更深入、更复杂的类加载器设置。如果您看到一个类的类加载器在一个和另一个中的显示方式不同,那么这很可能是一个原因。
I'm not knowledgeable in what exactly Eclipse is doing but I think it very likely that your class is notbeing loaded by the bootstrap classloader when being run from Eclipse but that Eclipse is attempting to make it seem that way.
我不知道 Eclipse 到底在做什么,但我认为您的类很可能在从 Eclipse 运行时没有被引导类加载器加载,但是 Eclipse 试图让它看起来像那样。
The bootstrap ClassLoader is static once the application is bootstrapped and you can't add jars or classes to it later unless Eclipse has overridden the implementation... in which case, there's yet another possible explanation.
一旦应用程序被引导,引导类加载器就是静态的,除非 Eclipse 覆盖了实现,否则你以后不能向它添加 jars 或类......在这种情况下,还有另一种可能的解释。
回答by Rocky
I had the same issue. But solved it using :-
我遇到过同样的问题。但是使用以下方法解决了它:-
<ClassName>.class.getClass().getResource(urlString);
Hope this helps others...
希望这可以帮助其他人...
回答by Satyendra
This is how it works . Whenever JVM try to load any class it's checks below conditions.
这就是它的工作原理。每当 JVM 尝试加载任何类时,它都会检查以下条件。
If Class is loaded from Bootstrap ClassPath i.e; jdk\jre\lib\rt.jar , BootStrap ClassLoader will be called.
如果 Class 是从 Bootstrap ClassPath 加载的,即;jdk\jre\lib\rt.jar ,BootStrap ClassLoader 将被调用。
If Class is loaded from Extension Classpath i.e; jdk\jre\lib\ext*.jar , Extension ClassLoader will be called.
如果从扩展类路径加载类,即;jdk\jre\lib\ext*.jar ,会调用扩展类加载器。
If Class is loaded from Application ClassPath i.e; as specified in Environment Variable , Application ClassLoader is called .
如果 Class 是从 Application ClassPath 加载的,即;如环境变量中所指定,应用程序类加载器被调用。
Since Bootstrap ClassLoader is not implemented in java , it's either implemented in c or c++ so there is no reference for it that's why it returns null . But Extension and Application class Loader is written in java so you will get the reference as sun.misc.Launcher$ExtClassLoader@someHexValue and sun.misc.Launcher$AppClassLoader@someHexValue .
由于 Bootstrap ClassLoader 没有在 java 中实现,它要么是在 c 或 c++ 中实现的,所以没有对它的引用,这就是它返回 null 的原因。但是 Extension 和 Application 类 Loader 是用 java 编写的,因此您将获得 sun.misc.Launcher$ExtClassLoader@someHexValue 和 sun.misc.Launcher$AppClassLoader@someHexValue 的引用。
So, if you do something like this System.out.println(String.class.getClassLoader()) you will get null since this class is been called by BootStrap ClassLoader, On the other hand if you do the same thing for a class in Ext or App Class path you will get $ExtClassLoader@someHexValue and sun.misc.Launcher$AppClassLoader@someHexValue respectively .
所以,如果你做这样的事情 System.out.println(String.class.getClassLoader()) 你会得到 null 因为这个类是由 BootStrap ClassLoader 调用的,另一方面,如果你对一个类做同样的事情Ext 或 App Class 路径,您将分别获得 $ExtClassLoader@someHexValue 和 sun.misc.Launcher$AppClassLoader@someHexValue 。
回答by ramidzkh
"This method will return null in such implementations if this class was loaded by the bootstrap class loader." - JavaDocat getClassLoader()
“如果此类由引导类加载器加载,则此方法将在此类实现中返回 null。” - getClassLoader() 中的JavaDoc
The null class loader is reserved for system classes for security purposes and can only be used if Class.forName(String name, boolean initialize, ClassLoader loader). If a class has a null ClassLoader, most security checks are not made.
出于安全目的,空类加载器是为系统类保留的,并且只能在 Class.forName(String name, boolean initialize, ClassLoader loader) 时使用。如果一个类的 ClassLoader 为空,则不会进行大多数安全检查。
回答by Kaushik Patel
Had similar issue. Solved by not using the getClass Method. Following worked for me.
有类似的问题。通过不使用 getClass 方法解决。以下为我工作。
<ClassName>.class.getClassLoader();