RaceBase
RaceBase

Reputation: 18868

How JRE picks up the duplicate classes

Assume there are two jars, both contain same class file with same fully qualified name. Now how JRE sees them, will it throw runtime exception? which I couldn't see in my testing.

How it picks up the class/which will be loaded and which one will be neglected?

Upvotes: 1

Views: 1013

Answers (4)

Caleryn
Caleryn

Reputation: 1084

A Single class loader will only ever load one of these although, there is no defined behaviour as to which one may be loaded. this can cause issues, including ClassNotFoundExceptions even when the two classes are actually exactly the same code prior to compilation. It will load from the first jar that is locates, but this can vary according to class loader and OS and may produce some odd side effects especially, in older servlet containers. This situation should be avoided whenever possible.

Upvotes: 0

Deepak Sharma
Deepak Sharma

Reputation: 4170

you can not use.. if using some IDE it will give you error and duplication or ambiguous class found.. you have to specify the class from which jar file u want to use.. if both having same name and u have added the reference of both in ur project or class.. and using the import statement for both.. it should give u the error..

Upvotes: 0

icza
icza

Reputation: 418505

It will not cause exceptions because when a class is first referenced (and is not yet loaded), the class loader will look for it in the class path and once it is found, it will be loaded and the search will stop.

Upvotes: 0

talex
talex

Reputation: 20542

Jre take class from first jar.

Upvotes: 2

Related Questions