Get manifest file from executable JAR

Say I have:
core.jar
client.jar (contains the main method) (uses core.jar)
super_mega_client.jar (uses core.jar, client.jar)

To run my program, I use "java -jar super_mega_client.jar"
How can I get the manifest file from "super_mega_client.jar" without knowing anything about this name and content?
Actualy I need to create a utility in core.jar that will work with a jar executable with java -jar ***. Jar.


OK, here is the correct question:
I have main.jar with the main method (say in the class my.app.Main )
I also have fisrt.jar (with some classes and resources) and second.jar (with some other classes and resources). Both do not have main classes, both have " main.jar " in CLASSPATH, both have a Main-Class property defined as " my.app.Main ".
I can launch my application by executing "java -jar first.jar" or "java -jar second.jar"
In my method my.app.Main.main(String[] args) (contained in main.jar ) I want to know the name of the executable jar (I want to get either first.jar "or" second.jar ")
How can i do this?

+4
source share
3 answers

In any bank declaring its main class, location and name are fixed by the standard up to META-INF/MANIFEST.MF

You can get the manifest file using the jar command that comes with the Java SDK (any zip tool that you can run from this command might be enough) by doing:

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

this will create the META-INF directory and place the manifest file in it. You can leave -v if you want less verbose output.

0
source

Not quite sure that I understand what you need here, but check out this article (for example)

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

You are requesting CLASSLOADER (so use an already loaded class and ask which class loader it uses, probably enough) to load the resource for you.

0
source

You can use the -verbose option of the java command to see the names of all loaded classes.

0
source

Source: https://habr.com/ru/post/1300718/


All Articles