Hi. I have two jar files, A and B. A is a component, B is a component too, but has some classes that have the same name as some classes in class A. The classes in component B are exptected to override the behaviour of classes in component A. Is there some way to instruct java to give priority to classes in component B when names overlap ?
The classes in component B are exptected to override the behaviour of classes in component A
That sound ugly. Can you explain why they do this?
Overriding methods is easy to do in Java, and doesn't require fixing the load order of classes. Also names in Java should never overlap if you properly package your class files (unless you have more than one version of a class). [ February 02, 2005: Message edited by: Paul Sturrock ]
As far as I know, per default Java searches for classes in jar files in exactly the sequence they appear in the class path. That is, if you put the jar file for B before the jar file for A in the classpath, it possibly already works. I'm not fully sure about it, though.
Nevertheless, Paul is right that it sounds ugly. It's probably even the way to hell.
Can you explain why you need to do this? [ February 02, 2005: Message edited by: Ilja Preuss ]
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Joined: Feb 09, 2005
Doesn't sound ugly to me.
There are plenty cases where you want to override badly written code (Like Hibernate using the wrong context to load resources).
When you package your application (including your "a.jar" and "b.jar" files) either in a jar, war or ear file, add /META-INF/MANIFEST.MF (that is directory+file) to your jar, war or ear.
The /META-INF/MANIFEST.MF file has to contain the following:
Manifest-Version: 1.0 Class-Path: b.jar a.jar
Be careful with upper/lower case. If "b.jar" is declared before "a.jar", the "b.jar" classes will override the "a.jar" classes with the same fully qualified name.