简体   繁体   English

Java:库中的库和类路径问题

[英]Java: Libraries within Libraries and Classpath Issues

We are having a discussion at work and an interesting point came up: 我们正在工作中进行讨论,并提出了一个有趣的观点:

Say you are developing a small library, call it somelib . 假设您正在开发一个小型库,请将其称为somelib Say that somelib needs to do some logging, but you don't want to reinvent the wheel, so you decide to use a 3rd party logging library. 假设somelib需要进行一些日志记录,但是您不想重新发明轮子,因此决定使用第三方日志记录库。

Additionally, you want to make integration of somelib as painless as possible, so you distribute a single JAR file (somelib.jar), which has the other logging JAR, call it logger.jar, embedded inside of it . 另外,您希望尽可能轻松地集成somelib,因此您分发了一个JAR文件(somelib.jar),该文件中包含另一个日志记录JAR,将其称为logger.jar 嵌入其中 Much like what Maven's jar-with-dependencies assembly does. 就像Maven的jar-with-dependencies程序所做的一样。

Now comes the issue. 现在来了问题。 Since your product is a library, what if your customer is using somelib and also happen to be using a different version of the same logging library on their own. 由于您的产品是一个库,因此如果您的客户正在使用somelib ,并且碰巧自己正在使用同一日志记录库的其他版本,该怎么办 Now we have a classpath problem. 现在我们有一个类路径问题。

This seems to me like it would be a common problem for people that write libraries, so what is the typical solution? 在我看来,这对于编写库的人来说将是一个普遍的问题,那么典型的解决方案是什么?

  1. Do they avoid using JAR bundling methods altogether? 他们是否避免完全使用JAR捆绑方法? Even if we do that, there is still an issue with a user's code expecting version X of the logging library, and somelib 's code expecting version Y. 即使我们这样做,期望日志记录库的版本X的用户代码和期望版本Y的somelib的代码仍然存在问题。

  2. Do they somehow insert a dummy package prefix so that the logger classes in somelib won't conflict? 他们是否以某种方式插入了虚拟包前缀,以便somelib中的logger类不会冲突?

  3. What about dynamic loading of the logger library? 如何动态加载记录器库? (though this still has versioning problems from 1.) (尽管这仍然存在版本1的版本问题。)

您可以考虑使用OSGI或等待JDK 8及其Jigsaw项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM