GoogleContainerTools/jib · error · IllegalArgumentException

The class file (${jarEntry}) is of an invalid format.

Error message

The class file (${jarEntry}) is of an invalid format.

What it means

determineJavaMajorVersion() reads the first class file found in the JAR to detect its Java version. It throws IllegalArgumentException when the class file's magic number is not 0xCAFEBABE, meaning the entry is not a valid Java class file and the version cannot be determined.

Source

Thrown at jib-cli/src/main/java/com/google/cloud/tools/jib/cli/ArtifactProcessors.java:151

   * the JAR.
   *
   * @param jarPath path to the jar
   * @return java version
   * @throws IOException if I/O exception thrown when opening the jar file
   */
  public static Integer determineJavaMajorVersion(Path jarPath) throws IOException {
    try (JarFile jarFile = new JarFile(jarPath.toFile())) {
      Enumeration<JarEntry> jarEntries = jarFile.entries();
      while (jarEntries.hasMoreElements()) {
        String jarEntry = jarEntries.nextElement().toString();
        if (jarEntry.endsWith(".class") && !jarEntry.endsWith("module-info.class")) {
          try (URLClassLoader loader = new URLClassLoader(new URL[] {jarPath.toUri().toURL()});
              DataInputStream classFile =
                  new DataInputStream(loader.getResourceAsStream(jarEntry))) {

            // Check magic number
            if (classFile.readInt() != 0xCAFEBABE) {
              throw new IllegalArgumentException(
                  "The class file (" + jarEntry + ") is of an invalid format.");
            }

            // Skip over minor version
            classFile.skipBytes(2);

            int majorVersion = classFile.readUnsignedShort();
            int javaVersion = (majorVersion - 45) + 1;
            return javaVersion;
          } catch (EOFException ex) {
            throw new IllegalArgumentException(
                "Reached end of class file ("
                    + jarEntry
                    + ") before being able to read the java major version. Make sure that the file is of the correct format.");
          }
        }
      }
      return VERSION_NOT_FOUND;

View on GitHub (pinned to fb949e2676)

Solutions

  1. Rebuild the JAR and verify it with `jar tf` / opening it in an IDE
  2. Ensure the JAR is a genuine Java artifact containing valid .class files
  3. Check for corruption: compare the artifact checksum with the one published by the build
Defensive patterns

Strategy: try-catch

Validate before calling

// verify the artifact is a real jar before processing
if (!Files.exists(jarPath) || Files.size(jarPath) < 100) throw new IOException("suspicious jar: " + jarPath);

Try / catch

try { int v = determineJavaMajorVersion(jarPath); } catch (IllegalArgumentException e) { /* report corrupt/invalid class file; rebuild or re-fetch the jar */ }

Prevention

When it happens

Trigger: The first .class entry in the JAR is corrupt, truncated, encrypted, or is not actually a class file (e.g. a mislabeled resource, a jar signed/obfuscated so the first entry is a manifest-relative fake .class, or a non-JVM artifact named .jar).

Common situations: Pointing jib at a fat jar produced by an unusual packager, a corrupted artifact from a partial upload, or a renamed zip that is not a JAR.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


AI-assisted analysis of GoogleContainerTools/jib@fb949e2676 (2026-09-06). Data as JSON: /api/errors/2d5beee8fd644310. Report an issue: GitHub.