elastic/elasticsearch · error · GradleException

Failed when reading jar file ${path}

Error message

Failed when reading jar file ${path}

What it means

Thrown as GradleException by assertAllESJarsAreModular()'s catch(IOException) when opening or reading an ES jar file with new JarFile(path.toFile()) fails. This is the I/O-error counterpart to the missing-module-info check: the jar exists but is corrupted, truncated, unreadable, or in an invalid zip format.

Source

Thrown at build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/InternalDistributionModuleCheckTaskProvider.java:122

                    } catch (IOException e) {
                        throw new UncheckedIOException(e);
                    }
                }
            });
        });
    }

    /** Checks that all expected ES jar files are modular, i.e. contain a module-info.class in their root. */
    private static void assertAllESJarsAreModular(Path libPath) throws IOException {
        try (var s = Files.walk(libPath, 1)) {
            s.filter(Files::isRegularFile).filter(isESJar).filter(isNotExcluded).sorted().forEach(path -> {
                try (JarFile jf = new JarFile(path.toFile())) {
                    JarEntry entry = jf.getJarEntry(MODULE_INFO);
                    if (entry == null) {
                        throw new GradleException(MODULE_INFO + " no found in " + path);
                    }
                } catch (IOException e) {
                    throw new GradleException("Failed when reading jar file " + path, e);
                }
            });
        }
    }

    /** Checks that all expected Elasticsearch modules are present. */
    private static void assertAllModulesPresent(Path libPath) {
        List<String> actualESModules = ModuleFinder.of(libPath).findAll().stream().filter(isESModule).map(toName).sorted().toList();
        if (actualESModules.equals(EXPECTED_ES_SERVER_MODULES) == false) {
            throw new GradleException(
                "expected modules " + listToString(EXPECTED_ES_SERVER_MODULES) + ", \nactual modules " + listToString(actualESModules)
            );
        }
    }

    // ####: eventually assert hashes, etc

    static String listToString(List<String> list) {

View on GitHub (pinned to db6a809a66)

Solutions

  1. Delete the offending jar and rebuild the distribution: rm <path> && ./gradlew :distribution:archives:<name>:build.
  2. Verify the file is a valid zip: unzip -l <path> or jar tf <path>.
  3. Check for symlinks or concurrent processes writing to lib/.
  4. If recurring, run with --info to see which task produced the corrupt jar.

Example fix

# before: truncated/corrupt jar in lib/
# throws: Failed when reading jar file .../lib/elasticsearch-server-9.0.0.jar

# after: clean and rebuild
rm .../lib/elasticsearch-server-9.0.0.jar
./gradlew :distribution:archives:linux-tar:assemble
Defensive patterns

Strategy: validation

Validate before calling

if (Files.size(path) < 22 || isValidZip(path) == false) {
    System.err.println("WARNING: jar appears corrupt/truncated: " + path);
}

Try / catch

try (JarFile jf = new JarFile(path.toFile())) {
    // use jf
} catch (IOException e) {
    throw new GradleException("Failed when reading jar file " + path, e);
}

Prevention

When it happens

Trigger: The try block at line 116 opens a new JarFile. If the file is not a valid zip/jar (truncated download, concurrent write, corrupted archive, zero-byte file), the IOException is caught at line 121 and rethrown as GradleException with the path.

Common situations: A previous build task was interrupted leaving a truncated jar in lib/; disk corruption; antivirus quarantined part of the jar; a symlink in lib/ pointing to a missing target; the jar is still being written by a concurrent process when checkModules runs.

Related errors


AI-assisted analysis of elastic/elasticsearch@db6a809a66 (2026-08-12). Data as JSON: /api/errors/ab9d1442e15c9446. Report an issue: GitHub.