如何从Java获取jar文件的主类名称?

发布于 2024-12-22 00:29:42 字数 69 浏览 4 评论 0原文

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

从中获得“主级”的最简单方法是什么?

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

公布 2024-12-29 00:29:42

我知道这是一个老问题,但至少对于 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() 方法返回的列表中。

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.

护你周全 2024-12-29 00:29:42

来自此处 - 列出 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) {}
    }
}

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) {}
    }
}
咋地 2024-12-29 00:29:42

这只有在 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");
}

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");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文