4

My project includes two jars (xyz-jre7.jar and xyz-jre8.jar) in the classpath, which contain classes with identical names. During runtime, my program seems to pick up the correct jar and classes with both JRE 7 and 8. Internally, how does Java know which class to use? In which file(s) are these checks made?

ExtremistEnigma
  • 239
  • 3
  • 12
  • Are these `maven` projects? Because you can define a java-version in them. – Yassin Hajaj Jun 08 '18 at 22:01
  • Since they have the same artifact IDs, I need to include them as a part of fileset. If I try include them normally (pom.xml), the one closest to the dependency tree gets picked up, but that is not the question here. The question is how does Java know which one to use when both are included. – ExtremistEnigma Jun 08 '18 at 22:06
  • What's the difference between those jars? How do you execute your app? – Gonzalo Matheu Jun 08 '18 at 22:42
  • I have one question. Why? Only one of them will ever be used. – user207421 Jun 09 '18 at 01:08

1 Answers1

1

The default ClassLoader load classes from jar files in non deterministic order. If there are two or more classes with same fully qualified name, the first one loaded wins.

Application server usually have custom ClassLoaders

Gonzalo Matheu
  • 8,984
  • 5
  • 35
  • 58