Home  >  Article  >  Java  >  Why Do JAR Files Created with `JarOutputStream` Fail to Load Libraries?

Why Do JAR Files Created with `JarOutputStream` Fail to Load Libraries?

Patricia Arquette
Patricia ArquetteOriginal
2024-10-28 19:09:29874browse

Why Do JAR Files Created with `JarOutputStream` Fail to Load Libraries?

Issue: Discrepancies in JAR File Creation Using JarOutputStream

Creating JAR files programmatically using java.util.jar.JarOutputStream often leads to inconsistencies. While the produced JAR files may appear valid and extractable, they face issues when loading libraries. Despite containing the required files, Java fails to locate them. This discrepancy between programmatically generated JAR files and those created using Sun's jar command-line tool necessitates an understanding of the underlying problem.

Solution: Adhering to Undocumented Quirks of JarOutputStream

Investigating the intricacies of JarOutputStream reveals three undocumented quirks that play a crucial role in successful JAR file creation:

  1. Directory Naming: Directory names must invariably end with a '/' slash.
  2. Path Delimitation: Paths within the JAR file must utilize forward slashes '/' instead of backslashes ''.
  3. Entry Origin: Entry paths should never commence with a '/' slash.

Revised Implementation:

To address these quirks and create JAR files correctly, the following code demonstrates the appropriate approach:

<code class="java">public void run() throws IOException {
    Manifest manifest = new Manifest();
    manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
    JarOutputStream target = new JarOutputStream(new FileOutputStream("output.jar"), manifest);
    add(new File("inputDirectory"), target);
    target.close();
}

private void add(File source, JarOutputStream target) throws IOException {
    String name = source.getPath().replace("\", "/");
    if (source.isDirectory()) {
        if (!name.endsWith("/")) {
            name += "/";
        }
        JarEntry entry = new JarEntry(name);
        entry.setTime(source.lastModified());
        target.putNextEntry(entry);
        target.closeEntry();
        for (File nestedFile : source.listFiles()) {
            add(nestedFile, target);
        }
    } else {
        JarEntry entry = new JarEntry(name);
        entry.setTime(source.lastModified());
        target.putNextEntry(entry);
        try (BufferedInputStream in = new BufferedInputStream(new FileInputStream(source))) {
            byte[] buffer = new byte[1024];
            while (true) {
                int count = in.read(buffer);
                if (count == -1)
                    break;
                target.write(buffer, 0, count);
            }
            target.closeEntry();
        }
    }
}</code>

By adhering to these guidelines, programmatically generated JAR files will accurately reflect their contents and eliminate loading issues encountered when extracting or using them.

The above is the detailed content of Why Do JAR Files Created with `JarOutputStream` Fail to Load Libraries?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn