集册 Java实例教程 从jar清单获取主类

从jar清单获取主类

欢马劈雪     最近更新时间:2020-01-02 10:19:05

500
从jar清单获取主类


//package com.nowjava;

import java.util.Map;

import java.util.jar.JarFile;//from n o w j a v a . c o m

import javax.swing.JOptionPane;


public class Main {

    /**

     * Gets main class from jar manifest

     * @param jar

     * @return

     */

    public static String getMainClass(JarFile jar) {

        try {

            Map<Object, Object> map = jar.getManifest().getMainAttributes();


            String mainClass = null;

            /** from 
            时 代 J a v a 公 众 号 - N o w J a v  a . c o m**/

            for (Object obj : map.keySet()) {

                if (obj.toString().equalsIgnoreCase("main-class")) {

                    mainClass = map.get(obj).toString();

                    break;

                }

            }


            return mainClass;

        } catch (Exception ex) {

            ex.printStackTrace();

            JOptionPane.showMessageDialog(

                    null,

                    "Failed loading manifest: "

                          
展开阅读全文