This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes 2 Files with the same Name in JAR - which wins? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "2 Files with the same Name in JAR - which wins?" Watch "2 Files with the same Name in JAR - which wins?" New topic
Author

2 Files with the same Name in JAR - which wins?

Flom Xanther
Ranch Hand

Joined: May 26, 2006
Posts: 44
Hi Ranchers,

I have a question:

We have 2 JAR-Files, say:
- a.jar
- z.jar

they contain the same structure and files with the same filenames BUT with different content.

Which of the JARs will win? Which Files will be used?

Thanks a lot!

Regards,
Flom
Chris Beckey
Ranch Hand

Joined: Jun 09, 2006
Posts: 116

I'm assuming your talking about which resource (class, properties, etc...) will get loaded?
1.) They may be loaded by different class loaders, in which case it is usually the highest ancestor class loader that wins. Unless the jars gets loaded as part of a webapp (in servlet 2.4 spec and above) then, usually, it is the webapp classloader.
2.) If it is the same classloader then I don't believe there to be a specified behavior. OTOH, if the files are listed in an environment or command line classpath I've yet to see a case where they are not loaded in the listed order.

Where are the jars? a webapp, on the classpath, in lib/ext, in an ear file, referred to in a manifest, etc ...?
Flom Xanther
Ranch Hand

Joined: May 26, 2006
Posts: 44
Hi Chris,

the files are in the "lib"-folder of a webapp.
I have to know, if it is predictable, how the resource are getting loaded.

Thanks,
Flom
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 16487
    
    2

Here's what the servlet specification (version 2.3) says:
The web application classloader must load classes from the WEB-INF/classes directory first, and then from library JARs in the WEB-INF/lib directory.
That's all it says. So I would say it's not predictable.
Chris Beckey
Ranch Hand

Joined: Jun 09, 2006
Posts: 116

I agree that it will not be predicatable. It may work consistently as long as no changes to the environment are made but the behavior could change with peripheral modifications (i.e. seemingly unrelated changes). This could include simple things like copying files or small code changes that affect the order of loading. Note that I'm kinda' taking an educated guess (knowing something about how class loaders work and making some assumptions about how specific instances would be written).
If you are working with an open source web/app server then you could look at its class loaders. They are not tremendously complex themselves (though complexity can arise from the relationships between many of them).
If the order is important then look at the classloader relationships of the web/app server and it should be possible to coax it into the desired behavior. Doing so is probably the most reliable suggestion if the jar files can't be changed.
Flom Xanther
Ranch Hand

Joined: May 26, 2006
Posts: 44
Thanks a lot Chris, Paul!

Regards,
Flom
 
jQuery in Action, 2nd edition
 
subject: 2 Files with the same Name in JAR - which wins?
 
Similar Threads
Question on class loading
comparing 2 jar files with timsramp and size
Class name clash
ant's checksum task on a .jar/.ear/.war fil
JDBC Driver connection