java: use two version of the same lib in one webapp

自古美人都是妖i 提交于 2019-12-22 05:23:24

问题


I'm facing the following problem: I have one module in my webapp that needs jaxb 1.x and the other module needs jaxb 2.x. The first module doesn't work with the new version of jaxb, and the opposite. How can I use these two jars in one project? Thanks.


回答1:


If you are using the JAXB reference implementation, then you can use your JAXB 1 models with the JAXB 2 runtime by including the jaxb1-impl.jar.

  • http://jaxb.java.net/faq/index.html#running1Apps



回答2:


For a regular application, usually very different versions use different package names. If this is the case, you can use them both at once without problem. However if they are the same, you can use jarjar to rename the package.

However since you are using a web container each application should use the version you deploy and not the other version. i.e. the web container works it out for you.

OSGi is another container which manages the versions much more explicitly and give you more control over these issues (however I believe you need it just for this)




回答3:


You have got a jar-hell issue. Generally speaking in normal java environment it's impossible to solve this problem. You have to force modularization into your project by using OSGI. Starting point: http://www.osgi.org/About/HowOSGi




回答4:


As Shaman said is imposible to resolve this issue.

Let's see: the servlet container JRE has only one classloader, and this classloader can load and use one class from jaxb or the other, but not both that will give you a classdefnotfound exception or something similar.

You can not solve this directly: you can get the code (is opensource) and change the package of one to another name so the classloader can use both. I do not recommend you this solution, is a bad one.

Better is that you migrate the code to use the most modern API (jaxb 2)



来源:https://stackoverflow.com/questions/7240529/java-use-two-version-of-the-same-lib-in-one-webapp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!