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

相关推荐
lee_curry7 小时前
第四章 jvm中的垃圾回收器
java·jvm·垃圾收集器
九转成圣8 小时前
Java 性能优化实战:如何将海量扁平数据高效转化为类目字典树?
java·开发语言·json
SmartRadio8 小时前
ESP32-S3 双模式切换实现:兼顾手机_路由器连接与WiFi长距离通信
开发语言·网络·智能手机·esp32·长距离wifi
laowangpython8 小时前
Rust 入门:GitHub 热门内存安全编程语言
开发语言·其他·rust·github
我叫汪枫8 小时前
在后台管理系统中,如何递归和选择保留的思路来过滤菜单
开发语言·javascript·node.js·ecmascript
_.Switch8 小时前
东方财富股票数据JS逆向:secids字段和AES加密实战
开发语言·前端·javascript·网络·爬虫·python·ecmascript
软件技术NINI8 小时前
webkit简介及工作流程
开发语言·前端·javascript·udp·ecmascript·webkit·yarn
Brendan_0018 小时前
JavaScript的Stomp.over
开发语言·javascript·ecmascript
念2348 小时前
f5 shape分析
开发语言·javascript·ecmascript
苍穹之跃8 小时前
某量JS逆向
开发语言·javascript·ecmascript