aspose file tools
The moose likes Java in General and the fly likes Custom order when loading jar files Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Custom order when loading jar files" Watch "Custom order when loading jar files" New topic
Author

Custom order when loading jar files

Diego Espada
Greenhorn

Joined: Feb 02, 2005
Posts: 2
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 ?

Thanks
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


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 ]

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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
shochan vanden
Greenhorn

Joined: Feb 09, 2005
Posts: 4
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.

Cheers
[ February 09, 2005: Message edited by: shochan vanden ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Custom order when loading jar files
 
Similar Threads
Developing under VAJ & compiling using Javac
Can we set classpath to scan the files in case of <context:component-scan>
ANT : trying not to include jar class files
the pictures are not loaded into an executable jar file
Exclude more than one class file when creating a jar