集册 Java实例教程 返回jar文件的名称;如果没有“ Main”,则返回null

返回jar文件的名称;如果没有“ Main”,则返回null

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

518
返回jar文件的名称;如果未定义“ Main-Class”清单属性,则返回null。

/*

 * Copyright 2013 Decebal Suiu

 * 

 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this work except in compliance with

 * the License. You may obtain a copy of the License in the LICENSE file, or at:

 * 

 * http://www.apache.org/licenses/LICENSE-2.0

 * 

 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on

 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the

 * specific language governing permissions and limitations under the License.

 */

//package com.nowjava;

import java.io.IOException;/*N o w J a v a . c o m - 时代Java 提供*/

import java.util.jar.Attributes;

import java.util.jar.JarFile;

import java.util.jar.Manifest;


public class Main {

    /**

     * Returns the name of the jar file, or null if no "Main-Class"

     * manifest attributes was defined.

     */

    public static String getMainClassName(String filename)

            throws IOException {

        JarFile jarfile = new JarFile(filename);

        Manifest manifest = jarfile.getManifest();


        Attributes mainAttributes = manifest.getMainAttributes();
        /
展开阅读全文