
jar(java archive)文件是java平台常用的打包格式,它将多个文件(如类文件、资源文件、元数据等)打包成一个单一的文件。在jar包中,meta-inf/manifest.mf文件扮演着核心元数据的角色。它包含了jar包的各种信息,例如版本号、入口类、类路径依赖、签名信息以及开发者自定义的属性等。java的java.util.jar.manifest类负责解析和管理这些元数据。
当我们需要向JAR包中添加或修改自定义属性时,通常会直接操作MANIFEST.MF文件。然而,即使通过文件系统API(如java.nio.file.FileSystem)成功写入了新的属性,并能通过文本编辑器或压缩工具(如7-Zip)验证其存在,Java的Manifest解析器却可能无法识别这些新添加的属性,导致Attributes.getValue(String)方法返回null。
问题的根源在于Manifest文件的格式规范。根据Java JAR文件规范,Manifest文件是一个由键值对组成的文本文件,每个键值对占一行,且每行都必须以一个换行符( )结束,包括文件的最后一行。
当开发者向Manifest文件追加自定义属性时,如果新添加的属性行后面缺少了必要的换行符,java.util.jar.Manifest解析器在读取时就会将其视为不完整的行,或者根本无法正确解析该属性。例如:
Manifest-Version: 1.0 Created-By: Apache Maven Deployments-Version: 1.2.3
在这个例子中,如果Deployments-Version: 1.2.3是文件的最后一行,并且其后没有换行符,那么Java的Manifest解析器将无法识别Deployments-Version这个属性。而像7-Zip这类工具仅仅是按照文本文件的方式读取内容,并不会执行严格的Manifest格式校验,因此它会显示出完整的文本内容。
立即学习“Java免费学习笔记(深入)”;
为了确保自定义属性能够被Java正确解析,在追加属性时,必须确保其后有一个换行符。
以下是修改Manifest文件并添加自定义属性的正确示例代码:
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.HashMap;
import java.util.Map;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
public class JarManifestModifier {
public static void main(String[] args) throws IOException, URISyntaxException {
// 假设要修改的JAR文件路径
File jar = new File("C:\path\to\your\auth-0.1.3.jar"); // 请替换为实际的JAR文件路径
String testVersion = "1.2.3";
// 确保JAR文件存在
if (!jar.exists()) {
System.err.println("Error: JAR file not found at " + jar.getAbsolutePath());
return;
}
// 1. 修改JAR包内的Manifest文件
Map<String, String> env = new HashMap<>();
env.put("create", "true"); // 允许创建文件系统,如果JAR不存在则创建(此处用于打开现有JAR)
// 挂载JAR文件系统
try (FileSystem fileSystem = FileSystems.newFileSystem(jarFileToURI(jar), env)) {
Path manifestPath = fileSystem.getPath("/META-INF/MANIFEST.MF");
// 读取原始Manifest内容
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
Files.copy(manifestPath, byteArrayOutputStream);
// 构建新的Manifest内容
StringBuilder manifestData = new StringBuilder(byteArrayOutputStream.toString().trim());
// 确保在添加新属性之前有一个换行符,并且新属性行本身也以换行符结束
// 这是解决问题的关键所在:新属性行后面必须有一个换行符
manifestData.append("
"); // 确保前一行结束
manifestData.append("Deployments-Version: ").append(testVersion).append("
"); // 新属性行及其后的换行符
// 将修改后的Manifest内容写回JAR包
Files.copy(new ByteArrayInputStream(manifestData.toString().getBytes()), manifestPath,
StandardCopyOption.REPLACE_EXISTING);
System.out.println("Manifest file updated successfully.");
} catch (Exception e) {
System.err.println("Error updating Manifest: " + e.getMessage());
e.printStackTrace();
return;
}
// 2. 验证并读取修改后的Manifest属性
System.out.println("
Attempting to read custom attribute from the modified JAR:");
try (JarFile jarFile = new JarFile(jar)) {
Manifest manifest = jarFile.getManifest();
if (manifest != null) {
String deploymentVersion = manifest.getMainAttributes().getValue("Deployments-Version");
System.out.println("Deployments-Version: " + deploymentVersion);
if (testVersion.equals(deploymentVersion)) {
System.out.println("Custom attribute read successfully!");
} else {
System.out.println("Custom attribute not read correctly or value mismatch.");
}
} else {
System.out.println("Manifest not found in the JAR.");
}
} catch (IOException e) {
System.err.println("Error reading JAR file: " + e.getMessage());
e.printStackTrace();
}
}
// 辅助方法:将File对象转换为JAR文件系统URI
// 来源于java.io.File,并进行了一些修改以适应JAR文件系统URI格式
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);
}
// 辅助方法:将路径字符串中的系统分隔符替换为斜杠,并添加前导斜杠
// 来源于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;
}
}在上述代码中,关键的修改位于:
manifestData.append("
"); // 确保前一行结束,如果Manifest文件末尾没有换行符,此行会补上
manifestData.append("Deployments-Version: ").append(testVersion).append("
"); // 新属性行及其后的换行符第一行manifestData.append(" ");是为了处理原始Manifest文件末尾可能没有换行符的情况,确保后续新添加的属性不会与前一行合并。第二行manifestData.append("Deployments-Version: ").append(testVersion).append(" ");则确保了新添加的Deployments-Version属性行本身也以换行符结束,这正是解决Manifest解析问题的核心。
向JAR包的Manifest文件添加自定义属性是一个常见的需求,但如果不了解Manifest文件的严格格式规范,特别是对行末换行符的要求,很容易遇到属性无法被Java Manifest API正确读取的问题。通过确保每个属性行都以换行符结束,包括文件中的最后一行,可以有效地解决这一问题,从而使自定义的元数据能够被应用程序正确识别和利用。
以上就是Java Jar包Manifest文件自定义属性读取:常见陷阱与解决方案的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号