views:

56

answers:

1

I had a question about Java Class Path variables.

If I have multiple jars with the same classes, which one does the jvm use at runtime. The first one listed in the Class Path, the last one, or is undefined?

Thanks

+5  A: 

It's the first one found. See Java Tip 105: Mastering the classpath with JWhich:

Of particular importance, and much consternation, the class loader will load classes in the order they appear in the classpath. Starting with the first classpath entry, the class loader visits each specified directory or archive file attempting to find the class to load. The first class it finds with the proper name is loaded, and any remaining classpath entries are ignored.

In practice it can get more complicated once you start putting multiple class loaders into the mix but basically it's first in, first out.

cletus
reference plz, and then you get an accepted answer and + 1 ;)
hhafez