简体   繁体   English

Java类加载器按什么顺序加载类?

[英]In which order does java classloader load classes?

I have two libraries of different versions in classpath. 我在类路径中有两个不同版本的库。 Which version will classloader load? 类加载器将加载哪个版本? How could I change it? 我该如何更改?

If you have 如果你有

lib-v1.jar:lib-v2.jar

then it's in order. 然后就可以了 lib-v1.jar will be loaded first (analogous to PATH ). lib-v1.jar将首先被加载(类似于PATH )。

I'd likely avoid this in practise (beyond uses for testing etc.) It can get confusing if you reference a library class in lib-v2.jar that's not in lib-v1.jar (possible if API's are retired). 我可能会在实践中避免这种情况(除了用于测试等用途之外),如果您在lib-v2.jar中引用不在lib-v2.jar中的库类(如果lib-v1.jar API,则可能会lib-v1.jar )。 That way you'd load the initial class from v2, and further common classes from v1, and these may not be compatible. 这样,您将从v2加载初始类,并从v1加载其他常见类,并且这些类可能不兼容。

这取决于将这些库列表提供给JVM的顺序。

Classloader has no information about your jar version. Classloader没有有关您的jar版本的信息。 Generally You should avoid this kind of situations. 通常,您应该避免这种情况。 You should hold the most recent version in your classpath 您应该在类路径中保存最新版本

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM