Java Manifest 文件的类路径及其如何确定相对目录
MANIFEST.MF 文件中的类路径如何确定相对位置?
1) 假设我有一个 JAR,其中包含 lib/somejar.jar
内的 jar,并且清单文件位于 META-INF/MANIFEST.MF
内。我将如何设置类路径......?是 Class-path: lib/somejar.jar
还是 ../lib/somejar.jar
?
2) 假设 somejar.jar 内部还有主 jar 所依赖的其他 jar。当然,somejar.jar 也有自己的 MANIFEST.MF 文件,其中包含正确设置的类路径字段。现在假设原始主 jar 文件在运行时需要访问这些 JAR 文件之一,只要 somejar.jar 位于 main.jar 的类路径上就可以这样做吗?或者相对目录是否混乱,因为主 jar 正在运行“somejar.jar”不再是根目录?
次要更新:这一切都将在应用程序服务器上的 Web 环境中运行。
How does the Class-path in the MANIFEST.MF file determine the relative location?
1)
Say I have a JAR with a jar inside lib/somejar.jar
and ofc the manifest file is inside META-INF/MANIFEST.MF
. How would I set the classpath....? Would it be Class-path: lib/somejar.jar
or ../lib/somejar.jar
?
2)
Let's say the somejar.jar also has other jars inside of it that the main jar depends on. And of course somejar.jar also has its own MANIFEST.MF file with a correctly set Class-path field. Now lets then say that the original main jar file, during run-time, needs to access one of those JAR files, will it be able to do so as long as somejar.jar is on main.jar's classpath? Or are the relative directories messed up since the main jar is running 'somejar.jar' isn't the root anymore?
minor update: This will all run in a web environment, on an application server.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当使用“plain vanilla”时,你不能在 JAR 中包含 JAR。您需要将其放在相对于主 JAR 的已知路径中,或者将 JAR 的内容提取到主 JAR 中。
但从 Eclipse 3.5 开始,您可以使用内置技巧来做到这一点。当您选择导出>时,选中第二个库处理选项。可运行的 JAR 文件。
当选择将所需的库打包到生成的 JAR 时,Eclipse 将添加一个特殊的类加载器,它将在执行主类之前透明地为您加载这些 JAR。它是在
JarRsrcLoader
。When doing it "plain vanilla", you can't have a JAR inside a JAR. You need to either put it outside in a known path relative to the main JAR, or extract the JAR's contents in the main JAR.
But since Eclipse 3.5 you can do this with a builtin trick. Check the 2nd Library Handling option when you choose Export > Runnable JAR file.
When choosing Package required libraries into generated JAR, then Eclipse will add a special classloader which will load those JAR's transparently for you before executing the main class. It's doing that with help of
JarRsrcLoader
.