JAR 的 MANIFEST.MF 文件在 Web 应用程序中使用?
我开发了一个实用程序库,将在我们的许多企业 Java 应用程序中使用。该库有许多额外的依赖项,这些依赖项也需要位于类路径上。我想避免强迫我们的开发人员向他们的 MANIFEST.MF 文件添加无数条目,而让他们只包含我的库。有什么方法可以让我的库的 MANIFEST.MF 文件引用其依赖项并让将使用我的库的企业应用程序选取它们?
我尝试使用文件系统上依赖项的完整路径在库的 MANIFEST.MF 文件中引用它们。那行不通。我的所有依赖项最终都会出现 ClassNotFoundException 错误。还有什么我应该尝试的吗?
I've developed a utility library that will be used in many of our enterprise Java applications. This library has numerous additional dependencies that also need to be on the classpath. I'd like to avoid forcing our developers to add a zillion entries to their MANIFEST.MF files, and let them instead just include my library. Is there any way that my library's MANIFEST.MF file can reference its dependencies and have them picked up by the enterprise applications that will be using my library?
I've tried referencing them in my library's MANIFEST.MF file using the full path to the dependencies on the filesystem. That didn't work. I end up with ClassNotFoundException errors for all of my dependencies. Is there something else I should be trying?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
创建 Web 应用程序时,通常会将其放入 WAR 文件中。这个想法是通过将 jar 添加到 WAR 内的 /WEB-INF/lib 文件夹,将所需的依赖项捆绑到该 WAR 文件中。 Web 容器(例如在 Java EE 应用程序服务器中)知道此结构并将在类路径中包含这些 jar。
如果您的库有其他依赖项,只需告诉用户这一点,然后在许可证允许的情况下将它们与您的库一起重新分发,或者告诉他们从哪里获取它们。当使用像 IDE、Ant with Ivy 或 Maven(或它们的组合)这样的不错的工具来创建 Web 应用程序时,处理和捆绑依赖关系应该没有问题。
When you create a web application, you'd normally put it in a WAR file. The idea is that you bundle the required dependencies in that WAR file, by adding the jars to the /WEB-INF/lib folder inside the WAR. Web containers (like in a Java EE application server) know of this structure and will include those jars on the classpath.
If your library has additional dependencies, just tell the users about it and either redistribute them with your library if the license allows it, or tell them where to obtain them. When using a decent tool for creating a web app like an IDE, Ant with Ivy, or Maven (or a combination of these), then handling and bundling dependencies should be no problem.
或者,只要您非常仔细坚持格式,即在每个“文件:”之前坚持恰好两个空格,则此操作有效:
Alternatively, this works so long as you stick to the format very carefully, i.e. stick to exactly two spaces before each "file:" etc:
我已经使用多种工具完成了此操作。这确实是一个可怕的黑客攻击,但似乎工作可靠。
给他们一个特殊的清单来使用。像这样的东西:
清单版本:1.0
主类:com.xxx.yyy.zzz.YourSpecialClassThatHacksTheClassPath
Real-Main-Class: com.ppp.qqq.TheirMainClass
在你的特殊类中,修改类路径(不容易),阅读清单“Real-Main-Class”条目(更容易一些)并启动他们的main 从中(一点也不难)。
显然这不适用于 .war 文件。
I've done this with a number of tools. It is a truly horrible hack but seems to work reliably.
Give them a special manifest to use. Something like:
Manifest-Version: 1.0
Main-Class: com.xxx.yyy.zzz.YourSpecialClassThatHacksTheClassPath
Real-Main-Class: com.ppp.qqq.TheirMainClass
In your special class, screw around with the classpath (not easy), read the manifest "Real-Main-Class" entry (a bit easier) and launch their main from that (not really difficult at all).
Obviously this will not work with a .war file.
即使我也有同样的问题。如上所述,解决方案是在 file:/ 后精确保留两个空格,在 .jar 文件后精确保留一个空格,最后按 Enter 键。
我知道这不是一个巧妙的解决方案,但它确实有效。享受。
Even I had the same problem. As mentioned above, the solution was to have exact two space after file:/ and one space after .jar file and at the end, press enter key.
I know this is not a neat solution, but it works. enjoy.