本文旨在解决从 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 解析器可能无法正确读取新添加的属性。
注意事项:
- 确保替换代码中的 “path/to/your/auth-0.1.3.jar” 为实际的 JAR 文件路径。
- 在修改 Manifest 文件后,建议重新构建 JAR 文件,以确保所有更改都已正确应用。
- 如果仍然无法读取自定义属性,请检查 Manifest 文件的编码格式是否正确(通常应为 UTF-8)。
总结:
当无法从 JAR 文件读取自定义 Manifest 属性时,首先检查 Manifest 文件的格式是否正确。确保在添加自定义属性后添加了额外的换行符。通过遵循这些步骤,可以有效地解决该问题,并确保 Java 程序能够正确读取 JAR 文件中的自定义 Manifest 属性。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END