本文介绍了类加载器如何在清单类路径中加载类引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用maven来构建一个带有外部类路径添加的jar,使用。

I used maven to built a jar with an external classpath additions using addClasspath.

当我使用 java -jar artifact.jar 运行该jar时,它能够从该主jar加载类以及来自libs目录中的所有jar。

When I run that jar using java -jar artifact.jar it is able to load classes from that main jar and from all jars in the libs directory.

但是如果我问系统属性 java.class.path 它只会列出主罐子。如果我向系统类加载器询问其URL( ClassLoader.getSystemClassLoader()。getURLs()),它也只会返回主jar。如果我问一些库中包含的任何类的类加载器,它将返回系统类加载器。

However if I ask the system property java.class.path it will only list the main jar. If I ask the system class loader for its urls (ClassLoader.getSystemClassLoader().getURLs()) it will also only return the main jar. If I ask any class contained in some library for its class loader it will return the system class loader.

系统类加载器如何加载这些类?

How is the system class loader able to load those classes?

它必须对这些库有一些了解才能从这些库中加载类。有没有办法要求它使用这种扩展类路径?

It has to have some knowledge about those libraries in order to load classes from those. Is there a way to ask it for this kind of "extended" classpath?

推荐答案

简短的回答是实现是部分太阳的内部运作,而不是通过公共手段提供。 getURLs()只返回传入的网址。答案较长,但只适用于大胆。

The short answer is that the implementation is part of Sun's internal workings and not available through public means. getURLs() will only ever return the URLs that are passed in. There is a longer answer but it is only for the daring.

使用调试器逐步调试Oracle JVM 8使我通过与OpenJDK6完全相同的结构,您可以看到它加载类路径的位置:

这篇关于类加载器如何在清单类路径中加载类引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 19:33