Java:类路径 JVM 上的多个资源中的哪一个?

2022-01-16 00:00:00 jvm loading resources java classpath

如果我在类路径上有多个同名文件(例如,我有多个 .jarlog4j.properties),JVM 遵循什么规则来选择一个?

If I have multiple files of the same name on classpath (e.g. I have multiple .jar with log4j.properties), what are the rules JVM follows to chose one?

推荐答案

使用-classpath选项指定资源(即通常是jar文件)的顺序来指定.类路径上较早"的资源优先于在它们之后指定的资源.这也可以在您的应用程序的清单文件中设置,然后您不需要提供 -classpath 选项.您可能需要查看这些文章,了解如何使用清单文件.

It is specified by the order in which the resources (i.e. usually jar files) are specified using -classpath option. Resources 'earlier' on the classpath take precedence over resources that are specified after them. This can be also set in the manifest file of your application and then you don't need to provide -classpath option. You may want to check these articles on how to work with manifest files.

可以在这里,JAR-class-path Classes部分描述了JAR文件搜索的逻辑.

The exhaustive description of "how classes are found" can be found here, where the section on JAR-class-path Classes describes the logic of JAR-files searching.

相关文章