将 JAR 文件打包为 WAR 文件
我有一系列依赖的Java项目。我想将它们打包成一个 JAR 文件,以便在我的 WAR 文件中使用。这些项目依赖于大量的外部库和项目,如log4j、apache-commons等。
我在Eclipse中选择所有项目并导出为JAR文件,然后将JAR文件复制到我的/WEB-INF/lib文件夹中我的 WAR,然后部署我的应用程序。我遇到以下问题:
ClassNotFoundException。 Web 应用程序找不到打包到我的 JAR 文件中的库。我通过将所有依赖库移出到 /WEB-INF/lib 文件夹并将类路径条目添加到 JAR 的 MANIFEST.MF 中解决了这个问题,这是一个非常痛苦的过程。
JAR 文件中的类找不到打包在 JAR 内的属性文件。如何解决这个问题?
这些问题有标准的解决方案吗?谢谢。
I have a series of dependent Java projects. I'd like to package them together into a single JAR file to be used in my WAR file. These projects depend on a large number of external libraries and projects such as log4j, apache-commons etc.
I select all the projects in Eclipse and export as a JAR file, then copy the JAR file into my /WEB-INF/lib folder of my WAR, then deploy my application. I have the following problems:
ClassNotFoundException. The web application cannot find the libraries packaged into my JAR file. I solved this problem by moving out all the dependent libraries into the /WEB-INF/lib folder and adding class-path entries into the MANIFEST.MF of the JAR, a thoroughly painful process.
The classes in the JAR file cannot find property files packaged inside the JAR. How do I solve this problem?
Is there a standard solution for these problems? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我建议您使用一些可以自动进行依赖管理的东西。如果您的构建脚本已经安装了 ant,那么 ivy 是一个很好的解决方案。如果您没有适当的构建脚本,maven 可能值得研究。
依赖管理会自动提取您直接使用的依赖项(jar)的传递依赖项。
I would recommend using something that does automatic dependency management for you. If you already have ant in place for your build script, ivy is a great solution. If you don't have a build script in place, maven might be worth looking into.
Dependency management automatically pulls in transitive dependencies for dependencies (jars) that you use directly.
我在部署时遇到了同样的问题。为了解决这个问题,我只是更改了项目的属性:
如果操作正确,这会自动将外部 .jar 库添加到您的 .war 文件中。
I had the same problem with the deployment. To solve I just changed the properties of the project:
If done correctly this'll automatically add the external .jar library to your .war file.
解决这个问题的标准方法是将所有依赖的 jar 文件放在 WEB-INF/lib 文件夹中。您正在寻找的是非标准解决方案。
其中,有很多。修改清单文件中的类路径就是其中之一。您还可以将它们放在容器共享库目录中或将它们放在任何位置并将它们添加到容器进程的全局类路径中。所有这些都可以,但没有一个被认为是最佳实践。
The standard solution to this problem is putting all jar files you depend on in the WEB-INF/lib folder. What you are looking for is a non-standard solution.
Of those, there are any number. Modifying classpath in the manifest file is one. You could also put them in the container shared library directory or put them anywhere and add them to the global classpath of the container process. All would work but none are considered best-practice.