hhafez
hhafez

Reputation: 39750

Are Java Class Paths First in First Searched?

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

Upvotes: 2

Views: 148

Answers (1)

cletus
cletus

Reputation: 625087

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.

Upvotes: 5

Related Questions