I want to load and execute external jar file using URLClassLoader.
What is the easiest way to get "Main-Class" from it?
Technically a jar file can contain more than one main class. When java executes a jar file, it looks in the META-INF/MANIFEST. MF file inside the jar to find the entrypoint. There is no direct command to get this information, but you can unpack the jar (it's just a zip file) and look into the manifest yourself.
You can open the jar file with winrar, this will show all the class files within, from there, you can drag them all into JD-GUI and decompile them all.
The main class can have any name, although typically it will just be called "Main".
I know this is an old question but, at least with the JDK 1.7, the previously proposed solutions did not seem to work. For this reason I am posting mine:
JarFile j = new JarFile(new File("jarfile.jar"));
String mainClassName = j.getManifest().getMainAttributes().getValue("Main-Class");
The reason why the other solutions did not work for me was because j.getManifest().getEntries()
turns out to not contain the Main-Class attribute, that was instead contained in the list returned by the getMainAttributes() method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With