4

I got two classes with the same package in different JARs. Until the previous version, both classes were identical, so i had no issues in loading them. Now, one of them has a new method added and if I want to access it, not only should I import the class with that package, i also need to make sure the jar with the correct class comes first in the classpath.

i.e. javac -classpath "%classpath%;a.jar;b.jar" MyClasses..

where a.jar has the class with my new method.

Now, how do i ensure this when my app goes to production, where it's deployed as an EAR file, with all the libraries under WEB-INF/lib?

How do I know which jar gets the preference over the other? Is it the alphabetical order like a.jar is given the first preference over b.jar?

I've read this safe-class-imports-from-jar-files thread and got to know about writing a custom classloader, but is there a better simpler solution that? Cos I'm just going to access this method in that whole JAR in this current project and writing a classloader seems a bit overkill.

And please don't ask me "Why the hell same class with same package in different JARs?" It's absolutely out of my control and it'll take some time to get this corrected.

Environment details: IBM WAS 6.1 on their 1.5 Java.

Please ask me more questions, if I don't make much sense. Thanks in advance!

1
  • OK guys, thanks for all your answers. In the end, as much as i hate to say, the only real fix, anyone can suggest, is removing the duplicates. Other than that, I've the option of adding my jar to the bootclasspath or the app server's classpath, but that option doesn't seem viable either as the deployment is not done by me. I'll accept an answer soon. Thanks a lot, again.
    – asgs
    Apr 28, 2011 at 12:04

6 Answers 6

3

You can try to change the startup script of your server and specify the jar with the correct class in the bootclasspath by using java -Xbootclasspath .... Otherwise there is no guarantee which one of the 2 jars will load up first.

3

As far as I know, the order of jars being loaded from WEB-INF/lib is arbitrary - I asked a similar question about JBOSS and got the reply ( from RedHat ) that it depends on the order that java.io.File.listFiles() returns them in ( and that is not a guaranteed order ).

A custom classloader would be an option, but have you considered repackaging the jars - removing the duplicated classes?

5
  • @DaveHowes Thanks, but like I said, it'll take some time to remove the duplicates as it has to go through some levels of approvals. Also, can you post the link to your question? It might be helpful.
    – asgs
    Apr 28, 2011 at 11:32
  • 2
    stackoverflow.com/questions/5727703/…
    – DaveH
    Apr 28, 2011 at 11:35
  • 2
    A hackey way of achieving this might be to put the "correct" jar on the app servers classpath and using "Parent First" classloading. It'd be a fragile approach though.
    – DaveH
    Apr 28, 2011 at 11:37
  • 1
    Right, it's a hack and it's fragile. But, likely there's nothing better to do.
    – david a.
    Apr 28, 2011 at 11:50
  • @DavesHowes I've written this custom classloader with the help of Sun tutorials and it tries to load the class i specify and printout its methods using reflection. But it loads my other jar's class when it comes first in the classpath. Am I doing anything wrong? I feel like even Custom ClassLoader will not be of any help if my Correct jar comes second in the classpath.
    – asgs
    Apr 29, 2011 at 8:20
2

Websphere allows you to specify the order in which classloaders of a particular application are inquired when searching for a class (the classloaders are hierarchically structured, from the topmost that loads JRE classes, down to classloader loading classes in your WAR).

During deployment of an app, you can specify if the order of inquiring the classloaders when searching for a class. There are two modes - Parent first (i.e. query the topmost classloader first) and parent last (query the app classloader first). This can be specified on both EAR and WAR level.

Packaging the duplicated jars to different locations in the app (e.g. one to EAR's classpath, the other to WAR's WEB-INF/lib) and setting the classloader orderING apropriately may solve your problem. However, if both your JARs have to be on the same level (e.g. WEB-INF/lib), then there's no way to specify which one will be used when loading the duplicated class.

3
  • Right on, both of them are at the application level :(
    – asgs
    Apr 28, 2011 at 11:43
  • 2
    place one on to EAR's classpath, or to appserver's ext classpath. IMO there's no other chance to do it.
    – david a.
    Apr 28, 2011 at 11:47
  • 2
    @asgs, this is basically a terrible situation for you. The only "real" solution is to fix the jars to clarify the dependency, unless you can do something truly funky like embedding OSGi in your web app. That would work, but the cure is worse than the disease, considering what the problem is. Apr 28, 2011 at 11:57
1

The order of the JARs in one application is likely to be alphabetical but the order of applications might not. Additionally, it depends on how the server handles classloading, i.e. whether it replaces existing classes or skips the new ones.

Although you already stated that, I'd still like to give that advice: Having the same class in multiple JARs deployed in one application (which could happen with versioned jars, for example) is always a bad idea. Your better off to invest the time to fix that instead of trying to mess with class loading.

1
  • @Andreas I read that, but as stated, I don't think messing around with class loading is worth it. This might be a reason to speed up the cleanup process (and the approvals that come with it). - Edit: and asgs didn't say there's absolute no way to fix it, it just seems to take a while.
    – Thomas
    Apr 28, 2011 at 11:43
1

This might come out to be pretty vague but I do remember resolving this issue a long time back by messing around with the WAS admin console for that given application and rearranging the relevant JAR files using their web UI. Not sure if this is an acceptable step in your case but worth a try in case everything else fails.

1
  • I'm guessing you're referring to the Class loading mode option available in the WAS' admin console.
    – asgs
    Apr 28, 2011 at 12:07
1

assuming you have some control over the deployment, fix the classloading yourself. combine the problematic jars yourself by unzipping them in reverse loading order into the same directory and then re-zipping into a new jar. then deploy the app with the new combo jar. no duplicate classes, problem solved.

or, just delete the dupe classes from the jars before deploying.

2
  • heh, you're right, but I'd be more than willing to do this, if I were allowed.
    – asgs
    Apr 28, 2011 at 12:00
  • @jtalhborn, I actually don't have any frigging control except checking in the files and waiting for the deployment to happpen :(
    – asgs
    Apr 28, 2011 at 12:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.