Java: Which of multiple resources on classpath JVM takes?

Ondra Žižka picture Ondra Žižka · Jul 11, 2011 · Viewed 32k times · Source

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?

Answer

mouser picture mouser · Jul 11, 2011

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.

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.