首页 > Java > java教程 > 正文

无法从 JAR 文件中读取自定义 Manifest 属性?原因与解决方案

碧海醫心
发布: 2025-08-06 15:24:15
原创
623人浏览过

无法从 jar 文件中读取自定义 manifest 属性?原因与解决方案

本文旨在解决从 JAR 文件读取自定义 Manifest 属性时遇到的问题。在修改 JAR 文件中的 Manifest 后,即使通过外部工具(如 7zip)确认属性已成功添加,Java 程序仍然可能无法读取这些属性。本文将分析导致此问题的原因,并提供相应的解决方案,确保自定义属性能够被正确读取。

在尝试向 JAR 文件添加自定义 Manifest 属性时,一个常见的问题是 Java 程序无法读取这些属性,即使这些属性已经存在于 JAR 文件的 META-INF/MANIFEST.MF 文件中。这通常是由于 Manifest 文件的格式不正确导致的,特别是缺少必要的换行符。

以下代码展示了如何使用 java.nio.file API 修改 JAR 文件的 Manifest:

import java.io.*;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.HashMap;
import java.util.Map;
import java.util.jar.JarFile;
import java.util.jar.Manifest;

public class ManifestModifier {

    public static void main(String[] args) throws IOException, URISyntaxException {
        File jar = new File("path/to/your/auth-0.1.3.jar"); // 替换为你的 JAR 文件路径
        String testVersion = "1.2.3";

        Map<String, String> env = new HashMap<>();
        env.put("create", "true");

        // Mount the jar
        try (FileSystem fileSystem = FileSystems.newFileSystem(jarFileToURI(jar), env)) {
            // Read the manifest
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            Path manifestPath = fileSystem.getPath("/META-INF/MANIFEST.MF");
            Files.copy(manifestPath, byteArrayOutputStream);

            // Convert the manifest bytes to a string and construct a string builder
            StringBuilder manifestData = new StringBuilder(byteArrayOutputStream.toString().trim());
            // Add the custom manifest attribute
            manifestData.append("\n");
            manifestData.append("Deployments-Version: ");
            manifestData.append(testVersion);
            manifestData.append("\n"); // 关键:添加额外的换行符

            // Write the manifest back to the jar
            Files.copy(new ByteArrayInputStream(manifestData.toString().getBytes()), manifestPath,
                    StandardCopyOption.REPLACE_EXISTING);

            // Try-with-resources closes the mounted jar
        }

        // This part should now work
        try (JarFile jarFile = new JarFile(jar)) {
            Manifest manifest = jarFile.getManifest();
            System.out.println(manifest.getMainAttributes().getValue("Deployments-Version"));
        }
    }

    // Stolen from java.io.File with some modifications
    private static URI jarFileToURI(File jarFile) throws URISyntaxException {
        String sp = slashify(jarFile.getAbsoluteFile().getPath(), false);
        if (sp.startsWith("//"))
            sp = "//" + sp;
        return new URI("jar:file", null, sp, null);
    }

    // Stolen from java.io.File;
    private static String slashify(String path, boolean isDirectory) {
        String p = path;
        if (File.separatorChar != '/')
            p = p.replace(File.separatorChar, '/');
        if (!p.startsWith("/"))
            p = "/" + p;
        if (!p.endsWith("/") && isDirectory)
            p = p + "/";
        return p;
    }
}
登录后复制

关键点在于添加自定义属性后,必须添加一个额外的换行符 (\n)。 Manifest 文件需要以换行符结尾,否则 Java 的 Manifest 解析器可能无法正确读取新添加的属性。

Robovision AI
Robovision AI

一个强大的视觉AI管理平台

Robovision AI 65
查看详情 Robovision AI

注意事项:

  • 确保替换代码中的 "path/to/your/auth-0.1.3.jar" 为实际的 JAR 文件路径。
  • 在修改 Manifest 文件后,建议重新构建 JAR 文件,以确保所有更改都已正确应用。
  • 如果仍然无法读取自定义属性,请检查 Manifest 文件的编码格式是否正确(通常应为 UTF-8)。

总结:

当无法从 JAR 文件读取自定义 Manifest 属性时,首先检查 Manifest 文件的格式是否正确。确保在添加自定义属性后添加了额外的换行符。通过遵循这些步骤,可以有效地解决该问题,并确保 Java 程序能够正确读取 JAR 文件中的自定义 Manifest 属性。

以上就是无法从 JAR 文件中读取自定义 Manifest 属性?原因与解决方案的详细内容,更多请关注php中文网其它相关文章!

相关标签:
最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号