【问题标题】:How to get name of main class of a jar file from Java?如何从 Java 中获取 jar 文件的主类的名称?
【发布时间】:2012-01-21 23:07:37
【问题描述】:

我想使用 URLClassLoader 加载和执行外部 jar 文件。

从中获取“Main-Class”的最简单方法是什么?

【问题讨论】:

  • 我不确定这就是您要查找的内容,但如果 jar 的组合正确,您会希望 jar 中有一个清单文件,指定哪个是主类。因此,您无需弄清楚这一点。
  • 我正在寻找无需手动读取该文件的简单方法。 java -jar 是否使用某些库函数从清单中读取属性?
  • 我不知道,很有可能是这样。官方文档只是说:在清单中设置 Main-Class 标头后,然后使用以下形式的 java 命令运行 JAR 文件: java -jar JAR-name Main 中指定的类的主要方法-类头被执行。如果您可以强制在该 jar 中存在清单,这将是最简单(并且可能更好)的方法。

标签: java classloader main


【解决方案1】:

我知道这是一个老问题,但至少在 JDK 1.7 中,之前提出的解决方案似乎不起作用。 出于这个原因,我发布了我的:

JarFile j = new JarFile(new File("jarfile.jar"));
String mainClassName = j.getManifest().getMainAttributes().getValue("Main-Class");

其他解决方案对我不起作用的原因是,j.getManifest().getEntries() 不包含 Main-Class 属性,而是包含在 getMainAttributes() 方法返回的列表中。

【讨论】:

    【解决方案2】:

    来自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) {}
        }
    }
    

    【讨论】:

    • 使用(-> (java.util.jar.JarFile. "myjarfile.jar") (.getManifest) (.getMainAttributes) (.getValue "Main-Class") )
    【解决方案3】:

    这只有在 jar 是自动执行的情况下才有可能;在这种情况下,主类将在清单文件中指定,键为 Main-Class:

    此处提供了一些参考信息: http://docs.oracle.com/javase/tutorial/deployment/jar/appman.html

    您需要下载 jarfile 然后使用java.util.JarFile 访问它;一些java代码可能是:

    JarFile jf = new JarFile(new File("downloaded-file.jar"));
    if(jf.getManifest().getEntries().containsKey("Main-Class")) {
        String mainClassName = jf.getManifest().getEntries().get("Main-Class");
    }
    

    【讨论】:

    • 是的,它是自动执行的。我想在不手动解析思想清单文件的情况下获得该名称。
    猜你喜欢
    • 1970-01-01
    • 2013-03-21
    • 1970-01-01
    • 2016-12-31
    • 1970-01-01
    • 1970-01-01
    • 2018-06-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多