您当前的位置: 首页 >  Java

杨林伟

暂无认证

  • 3浏览

    0关注

    3337博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Java解析jar包获取MainClass或属性配置

杨林伟 发布时间:2022-08-29 14:05:07 ,浏览量:3

以下是Java获取解析Jar包,获取MainClass以及属性配置的工具类,代码如下:

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.jar.Attributes;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.Manifest;

/**
 * jar包解析器
 *
 * @author : YangLinWei
 * @createTime: 2022/8/29 12:51 下午
 * @version: 1.0.0
 */
public class JarAnalyzer {

    /**
     * jar 文件路径
     */
    private String jarFilePath;

    /**
     * 构造函数
     *
     * @param jarFilePath 文件路径
     */
    public jarAnalyzer(String jarFilePath) {
        this.jarFilePath = jarFilePath;
    }

    /**
     * 获取jar包属性
     *
     * @return jar包所有属性
     * @throws IOException
     */
    public Map getJarAttrs() throws IOException {
        URL url = new File(this.jarFilePath).toURI().toURL();
        URLClassLoader urlClassLoader = new URLClassLoader(new URL[]{url});
        URL manifestUrl = urlClassLoader.findResource("META-INF/MANIFEST.MF");
        Manifest manifest = new Manifest(manifestUrl.openStream());
        Attributes mainAttributes = manifest.getMainAttributes();
        Map attrs = new HashMap();
        mainAttributes.forEach((key, value) -> {
            attrs.put(String.valueOf(key), String.valueOf(value));
        });
        return attrs;
    }

    /**
     * 获取入口类全路径名
     *
     * @return 入口类全路径名
     * @throws IOException
     */
    public String getProgamClass() throws IOException {
        for (String key : getJarAttrs().keySet()) {
            if ("program-class".equals(key)) {
                return getJarAttrs().get(key);
            }
        }
        return null;
    }

    /**
     * 获取jar包所有类名
     *
     * @return jar包所有类名
     * @throws IOException
     */
    public Set getAllClasses() throws IOException {
        File givenFile = new File(this.jarFilePath);
        Set classNames = new HashSet();
        try (JarFile jarFile = new JarFile(givenFile)) {
            Enumeration e = jarFile.entries();
            while (e.hasMoreElements()) {
                JarEntry jarEntry = e.nextElement();
                if (jarEntry.getName().endsWith(".class")) {
                    String className = jarEntry.getName()
                            .replace("/", ".")
                            .replace(".class", "");
                    classNames.add(className);
                }
            }
            return classNames;
        }
    }

}

单元测试代码:

 public static void main(String[] args) throws Exception {
        String jarPath = "/xxx/TopSpeedWindowing.jar";

        JarAnalyzer jarAnalyzer = new JarAnalyzer(jarPath);

        log.info("jar包所有属性:");
        jarAnalyzer.getJarAttrs().forEach((key, value) -> {
            log.info("key={},value={}", key, value);
        });

        log.info("MainClass -> {}", jarAnalyzer.getProgamClass());

        log.info("jar包含有的类:");
        jarAnalyzer.getAllClasses().forEach(clzz -> {
            log.info("className ->{}", clzz);
        });
    }

测试解析结果: 在这里插入图片描述

参考文献:

  • https://stackoverflow.com/questions/1272648/reading-my-own-jars-manifest
  • https://www.baeldung.com/jar-file-get-class-names
关注
打赏
1662376985
查看更多评论
立即登录/注册

微信扫码登录

0.1095s