Java 清单文件的类路径以及它如何确定相对目录 [英] Java Manifest file's class path and how it determines relative dirs

查看:19
本文介绍了Java 清单文件的类路径以及它如何确定相对目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MANIFEST.MF 文件中的 Class-path 如何确定相对位置?

How does the Class-path in the MANIFEST.MF file determine the relative location?

1)假设我在 lib/somejar.jar 中有一个带有 jar 的 JAR,并且清单文件在 META-INF/MANIFEST.MF 内.我将如何设置类路径....?是 Class-path: lib/somejar.jar 还是 ../lib/somejar.jar ?

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)假设 somejar.jar 里面还有主 jar 所依赖的其他 jar.当然 somejar.jar 也有自己的 MANIFEST.MF 文件,其中包含正确设置的 Class-path 字段.现在假设原始主 jar 文件在运行时需要访问这些 JAR 文件之一,只要 somejar.jar 位于 main.jar 的类路径上,它是否能够这样做?或者由于主 jar 正在运行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?

小更新:这一切都将在应用服务器上的 Web 环境中运行.

minor update: This will all run in a web environment, on an application server.

推荐答案

在普通"的情况下,您不能在 JAR 中包含 JAR.您需要将它放在相对于主 JAR 的已知路径之外,在主 JAR 中提取 JAR 的内容.

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.

但是从 Eclipse 3.5 开始,您可以使用内置技巧来做到这一点.当您选择 Export > Runnable JAR file 时,请选中第二个 Library Handling 选项.

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.它是在 JarRsrcLoader.

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.

这篇关于Java 清单文件的类路径以及它如何确定相对目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆