se7entyse7en
se7entyse7en

Reputation: 4294

Java - Same class in build classpath and runtime classpath priority

I'm not a Java expert, and I have a doubt about class resolution in case of conflict. Consider the scenario where I have two jar files external-library-0.0.1, external-library-0.0.2 both having a class Foo.

If I build with only external-library-0.0.1.jar in the build classpath, and then I add external-library-0.0.2.jar in the runtime classpath, which class Foo will be used?

Upvotes: 3

Views: 322

Answers (2)

Raffaele
Raffaele

Reputation: 20885

From the programmer point of view, assume random. Never put more than one definition of the same class on the classpath, neither at compile time nor at runtime.

One can point that sometimes there are tools that try to offer guarantees about load order, but rely on it makes your application inherently fragile, and this kind of bug is really hard to fix when you need to.

Upvotes: 1

peter.petrov
peter.petrov

Reputation: 39457

The one which is on the runtime classpath will be used.
The other one is simply not known to the JVM at runtime.

If there're any discrepancies (e.g. a method does not exist), you'll get an error at runtime.

Upvotes: 3

Related Questions