我用 7-zip 打开了一个 .jmod 文件,我可以看到内容。我尝试用 ZipInputStream 以编程方式读取它,但它不起作用:有人知道怎么做吗?
中没有文档JEP 261:模块系统 https://openjdk.java.net/jeps/261关于 JMOD 文件使用的格式。据我所知,这不是一个疏忽,因为将格式保留为实现细节意味着他们可以随时更改格式,恕不另行通知。话虽如此,目前 JMOD 文件似乎是以 ZIP 格式打包的;这个另一个答案 https://stackoverflow.com/a/44736159/6395627引用以下内容JEP 261:
JMOD 文件的最终格式是一个悬而未决的问题,但目前它基于 ZIP 文件。
但是,我在任何地方都找不到这句话JEP 261。它看起来来自该规范的旧版本 - 至少,我在历史中发现了类似的措辞JDK-8061972 https://bugs.openjdk.java.net/browse/JDK-8061972(与 JEP 相关的问题)。
这意味着您暂时应该能够使用任何允许读取 ZIP 文件的 API 来读取 JMOD 文件。例如,您可以使用以下其中一项:
-
The java.util.zip https://docs.oracle.com/en/java/javase/13/docs/api/java.base/java/util/zip/package-summary.html API:
import java.io.File;
import java.io.IOException;
import java.util.zip.ZipFile;
public class Main {
public static void main(String[] args) throws IOException {
var jmodFile = new File(args[0]).getAbsoluteFile();
System.out.println("Listing entries in JMOD file: " + jmodFile);
try (var zipFile = new ZipFile(jmodFile)) {
for (var entries = zipFile.entries(); entries.hasMoreElements(); ) {
System.out.println(entries.nextElement());
}
}
}
}
Note: To read the contents of an entry, see ZipFile#getInputStream(ZipEntry) https://docs.oracle.com/en/java/javase/13/docs/api/java.base/java/util/zip/ZipFile.html#getInputStream(java.util.zip.ZipEntry).
-
The ZIP 文件系统提供程序 https://docs.oracle.com/en/java/javase/13/docs/api/jdk.zipfs/module-summary.html API:
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
public class Main {
public static void main(String[] args) throws IOException {
var jmodFile = Path.of(args[0]).toAbsolutePath().normalize();
System.out.println("Listing entries in JMOD file: " + jmodFile);
try (var fileSystem = FileSystems.newFileSystem(jmodFile)) {
Files.walk(fileSystem.getRootDirectories().iterator().next())
.forEachOrdered(System.out::println);
}
}
}
Note: To read the contents of an entry, use one of the many methods provided by the java.nio.file.Files https://docs.oracle.com/en/java/javase/13/docs/api/java.base/java/nio/file/Files.html class.
Note: The Path#of(String,String...) https://docs.oracle.com/en/java/javase/13/docs/api/java.base/java/nio/file/Path.html#of(java.lang.String,java.lang.String...) method was added in Java 11 and the FileSystems#newFileSystem(Path) https://docs.oracle.com/en/java/javase/13/docs/api/java.base/java/nio/file/FileSystems.html#newFileSystem(java.nio.file.Path) method was added in Java 13. Replace those method calls if using an older version of Java.
不过,还是要重申一下:JMOD 文件使用的格式没有记录,并且可能会更改,恕不另行通知。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)