12

I want to load and execute external jar file using URLClassLoader.

What is the easiest way to get "Main-Class" from it?

Vi.
  • 37,014
  • 18
  • 93
  • 148
  • 3
    I am not sure that's what you're looking for, but if the jar is composed correctly, you would expect there's a manifest file in the jar, that specifies which is the main class. Thus you won't need to figure that out. – hovanessyan Dec 16 '11 at 12:09
  • I'm looking for simple ways without manual reading that file. Does `java -jar` use some library function to read attributes from manifest? – Vi. Dec 16 '11 at 12:13
  • I don't know, it's very likely it is the case. Official doc just says: After you have set the Main-Class header in the manifest, you then run the JAR file using the following form of the java command: java -jar JAR-name The main method of the class specified in the Main-Class header is executed. If you can enforce presence of manifest in that jar, this will be easiest (and probably better) way to go. – hovanessyan Dec 16 '11 at 12:17

3 Answers3

7

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.

ma1069
  • 100
  • 1
  • 8
5

This will only be possible if the jar is self-executing; in which case the main class will be specified in the manifest file with the key Main-Class:

Some reference information is provided here: http://docs.oracle.com/javase/tutorial/deployment/jar/appman.html

You will need to download the jarfile then use java.util.JarFile to access it; some java code for this might be:

JarFile jf = new JarFile(new File("downloaded-file.jar"));
if(jf.getManifest().getEntries().containsKey("Main-Class")) {
    String mainClassName = jf.getManifest().getEntries().get("Main-Class");
}
mcfinnigan
  • 11,442
  • 35
  • 28
  • Yes, it is self-executing. I want to get that name without manually parsing thought manifest file. – Vi. Dec 16 '11 at 12:11
5

From here - Listing the main attributes of a jarfile

import java.util.*;
import java.util.jar.*;
import java.io.*;

public class MainJarAtr{
    public static void main(String[] args){
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        try {
            System.out.print("Enter jar file name: ");
            String filename = in.readLine();
            if(!filename.endsWith(".jar")){
                System.out.println("File not in jar format.");
                System.exit(0);
            }

            File file = new File(filename);
            if (file.exists()){
                // Open the JAR file
                JarFile jarfile = new JarFile(filename);

                // Get the manifest
                Manifest manifest = jarfile.getManifest();

                // Get the main attributes in the manifest
                Attributes attrs = (Attributes)manifest.getMainAttributes();

                // Enumerate each attribute
                for (Iterator it=attrs.keySet().iterator(); it.hasNext(); ) {
                    // Get attribute name
                    Attributes.Name attrName = (Attributes.Name)it.next();
                    System.out.print(attrName + ": ");

                    // Get attribute value
                    String attrValue = attrs.getValue(attrName);
                    System.out.print(attrValue);
                    System.out.println();
                }
            }
            else{
                System.out.print("File not found.");
                System.exit(0);
            }
        }
        catch (IOException e) {}
    }
}
Preet Sangha
  • 64,563
  • 18
  • 145
  • 216
  • 1
    Using `(-> (java.util.jar.JarFile. "myjarfile.jar") (.getManifest) (.getMainAttributes) (.getValue "Main-Class") )` – Vi. Dec 16 '11 at 13:58