简体   繁体   English

从已执行的JAR获取清单文件

[英]Get manifest-file from executed JAR

Let's say I have: 比方说我有:
core.jar core.jar添加
client.jar (contains main-method) (uses core.jar) client.jar(包含main方法)(使用core.jar)
super_mega_client.jar (uses core.jar, client.jar) super_mega_client.jar(使用core.jar,client.jar)

To run my program I use "java -jar super_mega_client.jar" 要运行我的程序,我使用“java -jar super_mega_client.jar”
How could I get the manifest-file from "super_mega_client.jar" knowing nothing about it's name and content? 我怎么能从“super_mega_client.jar”获取清单文件,而对它的名称和内容一无所知?
Actualy I need to create an util in core.jar which will work with the jar, executed with "java -jar ***.jar" Actualy我需要在core.jar中创建一个util,它将与jar一起使用,用“java -jar ***。jar”执行


OK, here is proper question: 好的,这是正确的问题:
I have main.jar with main-method (lets say in my.app.Main class) 我有main.jar与main方法(让我们说在my.app.Main类)
I also have fisrt.jar (with some classes and resources) and second.jar (with some other classes and resources). 我还有fisrt.jar (带有一些类和资源)和second.jar (带有一些其他类和资源)。 Both have no main-classes, both have " main.jar " in CLASSPATH, both have Main-Class property defined as " my.app.Main ". 两者都没有主类,在CLASSPATH中都有“ main.jar ”,两者都将Main-Class属性定义为“ my.app.Main ”。
I can run my app by executing " java -jar first.jar " or " java -jar second.jar " 我可以通过执行“ java -jar first.jar ”或“ java -jar second.jar ”来运行我的应用程序
In my my.app.Main.main(String[] args) method (contained in main.jar ) I want to know the name of the executed jar (I want to get either " first.jar " or " second.jar ") 在我的my.app.Main.main(String[] args)方法(包含在main.jar中 )我想知道执行的jar的名称(我想得到“ first.jar ”或“ second.jar ” )
How could I do it? 我怎么能这样做?

In any jar that declares its main class, the location and name is fixed by the standard to META-INF/MANIFEST.MF 在声明其主类的任何jar中,位置和名称由标准修复为META-INF/MANIFEST.MF

You can retrieve the manifest file using the jar command that comes with the Java SDK (any zip tool that you can run from the command might suffice too) by running: 您可以使用Java SDK附带的jar命令检索清单文件(您可以从命令运行的任何zip工具也可以运行):

jar -xvf anyjar.jar META-INF/MANIFEST.MF

this will create the META-INF directory and put the manifest file in it. 这将创建META-INF目录并将清单文件放入其中。 You can leave out the -v if you want less verbose output. 如果你想要更简洁的输出,你可以省略-v。

Not quite sure I understand what you need here, but check out this article (for instance) 我不太清楚我明白你需要什么,但看看这篇文章(例如)

http://java.sun.com/j2se/1.5.0/docs/guide/lang/resources.html http://java.sun.com/j2se/1.5.0/docs/guide/lang/resources.html

You ask the CLASSLOADER (so use a class you have loaded already and request what classloader it used is probably good enough) to load the resource for you. 你问CLASSLOADER(所以使用你已经加载的类并请求它使用的类加载器可能已经足够好了)为你加载资源。

您可以使用java命令的-verbose选项查看所有已加载类的名称。

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

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