开发者

Will using multiple version of a jar in an application cause problems?

I came across an application in which multiple versions of jar files are included. For instance commons-fileupload-1.8.jar and commons-fileupload-1.6.jar.

Would this cause any issues?

Thanks, Raghuram开发者_如何学编程


Yes, that's a bad idea. What will probably happen if you're lucky is that whichever of the two versions that comes first in the classpath will satisfy all the references. If that happens, then the other versions of the .jar file won't matter at all. However, old code that relies on an old version of the library might incorrectly pick up new versions of some classes, and so all sorts of weird bad things can happen.

Now, in an application with many separate class loaders, such a thing might work out, as long as the separate subsystems with separate class loaders keep the different versions separated. If you're talking about multiple references to a .jar in the system classpath, however, then it's not a case of multiple class loaders.


In my experience, yes it will. The jar that gets used will be the one that is loaded first and that is based on the class loader and not, I think, in a guaranteed order. So that means that some code might be depending on a feature in version 1.8 and then 1.6 gets loaded and throws an exception when you try to use it.


There will only be issues if both versions are actually loaded through the same class loader, e.g. by both appearing on the regular classpath.

It can be made to work if you load the different versions through separate class loaders. Presumably the application you looked at is doing this. Or they just upgraded the JAR and forgot to delete the old version.


Definitely and it might give you different results sometimes depending on the app server and sometimes depending on the packaging.

If your application uses a class say X which is in both jars, the X.class one of them will be loaded by the classloader, and lets say that needs a class Y which is in both jars again one of them will be loaded (usually the first) but there is no guarantee that they will be from same jar.

So if there are two versions of same jar you need to inspect why this is happening and try and remove one of them. (If you are using maven there are different ways of achieving this)


yes it causes problems because only one of them will actually be used depending on which one gets loaded by the class loader(s) and what order they are loaded.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜