Java读写Jar

Java提供了读写jar的类库Java.util.jar,Java获取解析jar包的工具类如下:

java 复制代码
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包解析器
 *
 */
public class JarAnalyzer {

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

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

    /**
     * 获取jar包属性
     *
     * @return jar包所有属性
     * @throws IOException
     */
    public Map<String, String> 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<String, String> 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<String> getAllClasses() throws IOException {
        File givenFile = new File(this.jarFilePath);
        Set<String> classNames = new HashSet<>();
        try (JarFile jarFile = new JarFile(givenFile)) {
            Enumeration<JarEntry> 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;
        }
    }

}

测试类

java 复制代码
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

相关推荐
ejinxian2 分钟前
PHP 超文本预处理器 发布 8.5 版本
开发语言·php
Codebee6 分钟前
“自举开发“范式:OneCode如何用低代码重构自身工具链
java·人工智能·架构
程序无bug22 分钟前
手写Spring框架
java·后端
程序无bug24 分钟前
Spring 面向切面编程AOP 详细讲解
java·前端
软件黑马王子29 分钟前
C#系统学习第八章——字符串
开发语言·学习·c#
阿蒙Amon30 分钟前
C#读写文件:多种方式详解
开发语言·数据库·c#
全干engineer35 分钟前
Spring Boot 实现主表+明细表 Excel 导出(EasyPOI 实战)
java·spring boot·后端·excel·easypoi·excel导出
Da_秀38 分钟前
软件工程中耦合度
开发语言·后端·架构·软件工程
Fireworkitte1 小时前
Java 中导出包含多个 Sheet 的 Excel 文件
java·开发语言·excel
GodKeyNet1 小时前
设计模式-责任链模式
java·设计模式·责任链模式