简体   繁体   English

getClass().getClassLoader() 是null,为什么?

[英]getClass().getClassLoader() is null, why?

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."我的问题是“任何人都知道为什么相同的 class 在通过 Eclipse 启动时会返回 null 而在从命令行加载时会返回 class 加载程序。”

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 中加载 class 的建议。我不知道为什么会这样。

Citing the API doc :引用API 文档

Some implementations may use null to represent the bootstrap class loader.一些实现可能使用 null 来表示引导类加载器。 This method will return null in such implementations if this class was loaded by the bootstrap class loader.如果此类由引导类加载器加载,则此方法将在此类实现中返回 null。

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 ie;如果 Class 是从 Bootstrap ClassPath 加载的,即; jdk\\jre\\lib\\rt.jar , BootStrap ClassLoader will be called. jdk\\jre\\lib\\rt.jar ,BootStrap ClassLoader 将被调用。

If Class is loaded from Extension Classpath ie;如果从扩展类路径加载类,即; jdk\\jre\\lib\\ext*.jar , Extension ClassLoader will be called. jdk\\jre\\lib\\ext*.jar ,会调用扩展类加载器。

If Class is loaded from Application ClassPath ie;如果 Class 是从 Application ClassPath 加载的,即; as specified in Environment Variable , Application ClassLoader is called .如环境变量中所指定,应用程序类加载器被调用。

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 .由于 Bootstrap ClassLoader 没有在 java 中实现,它要么是在 c 或 c++ 中实现的,所以没有对它的引用,这就是它返回 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 .但是 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 。

This method will return null in such implementations if this class was loaded by the bootstrap class loader.如果此类由引导类加载器加载,则此方法将在此类实现中返回 null。

https://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Class.html#getClassLoader() https://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Class.html#getClassLoader()

One thing is certain, Eclipse has a deeper and more complicated classloader setup than when you are running from the command line.有一点是肯定的,与从命令行运行相比,Eclipse 具有更深入、更复杂的类加载器设置。 If you are seeing differences in how a class's classloader appears in one versus the other then that is a pretty likely reason.如果您看到一个类的类加载器在一个和另一个中的显示方式不同,那么这很可能是一个原因。

I'm not knowledgeable in what exactly Eclipse is doing but I think it very likely that your class is not being 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 或类......在这种情况下,还有另一种可能的解释。

Had similar issue.有类似的问题。 Solved by not using the getClass Method.通过不使用 getClass 方法解决。 Following worked for me.以下为我工作。

<ClassName>.class.getClassLoader();

The answer by @dilou points in the right direction. @dilou 的回答指出了正确的方向。 Check the settings of your run configuration in Eclipse: Go to the Classpath tab and check where you added the dependency.检查Eclipse中运行配置的设置:Go到Classpath选项卡并检查添加依赖项的位置。 There is two categories:有两类:

  • Bootstrap Entries引导条目
  • User Entries用户条目

If a dependency is added to the first category then the bootstrap classloader is used and the problem mentioned on most of the other answers happens: It is returned as null.如果将依赖项添加到第一个类别,则会使用引导classloader ,并且大多数其他答案中提到的问题都会发生:它返回为 null。

So to solve the problem, move your dependency to the "User Entries" section.因此,要解决该问题,请将您的依赖项移至“用户条目”部分。

I had the same issue.我有同样的问题。 But solved it using :-但是使用以下方法解决了它:-

<ClassName>.class.getClass().getResource(urlString);

Hope this helps others...希望这可以帮助其他人...

"This method will return null in such implementations if this class was loaded by the bootstrap class loader." “如果此类由引导类加载器加载,则此方法将在此类实现中返回 null。” - JavaDoc at getClassLoader() - 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).出于安全目的,空类加载器是为系统类保留的,并且只能在 Class.forName(String name, boolean initialize, ClassLoader loader) 时使用。 If a class has a null ClassLoader, most security checks are not made.如果一个类的 ClassLoader 为空,则不会进行大多数安全检查。

It may be helpful, in order to know what Eclipse does exactly in terms of classpath setting, when launching an application, to click on the "Show Command Line" button under the application classpath setting.在启动应用程序时,为了了解 Eclipse 在类路径设置方面究竟做了什么,单击应用程序类路径设置下的“显示命令行”按钮可能会有所帮助。 (I am using Eclipse 2020-06). (我正在使用 Eclipse 2020-06)。 For me, it showed that the jar file containing the class calling <ClassName>.class.getClassLoader();对我来说,它显示了包含调用<ClassName>.class.getClassLoader();类的 jar 文件<ClassName>.class.getClassLoader(); which was returning null, was actually preceeded by返回null,实际上前面是
-Xbootclasspath/a: -Xbootclasspath/a:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 this.getClass()。getClassLoader()。getResourceAsStream始终返回null - this.getClass().getClassLoader().getResourceAsStream always returning null Java getClass()。getClassLoader()。getResource(“ /My_Form.xsd”)返回null - Java getClass().getClassLoader().getResource( “/My_Form.xsd”) returns null getClass().getClassLoader().getResourceAsStream() 总是返回 null,资源在 application.jar 中 - getClass().getClassLoader().getResourceAsStream() always returns null, resource is in application.jar this.getClass()。getClassLoader()和ClassLoader - this.getClass().getClassLoader() and ClassLoader getClass()。getClassLoader()。getResourceAsStream()正在缓存资源 - getClass().getClassLoader().getResourceAsStream() is caching the resource this.getClass()。getClassLoader()。getResource()==异常 - this.getClass().getClassLoader().getResource() == Exception getClass()。getClassLoader()。getResourceAsStream引发NullPointerException - getClass().getClassLoader().getResourceAsStream is throwing a NullPointerException 即使路径正确,getClass()。getClassLoader()。getResource(path)始终返回null - getClass().getClassLoader().getResource(path) always return null, even when the path is correct java jar getClass().getClassLoader().getResourceAsStream 返回空指针异常 - java jar getClass().getClassLoader().getResourceAsStream return nullpointerexception Java OutputStream 等价于 getClass().getClassLoader().getResourceAsStream() - Java OutputStream equivalent to getClass().getClassLoader().getResourceAsStream()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM