apache/flink · error · RuntimeException

Could not instantiate type '{}' due to an unspecified except

Error message

Could not instantiate type '{}' due to an unspecified exception: {}

What it means

In InstantiationUtil.instantiate(Class), clazz.newInstance() throwing InstantiationException or IllegalAccessException first triggers checkForInstantiation(clazz) to report common causes; if none apply, the method throws RuntimeException('Could not instantiate type X due to an unspecified exception: msg'). 'Unspecified' here usually means the no-arg constructor is not accessible to the caller — e.g. package-private or protected constructor with a security/module restriction.

Source

Thrown at flink-core/src/main/java/org/apache/flink/util/InstantiationUtil.java:317

     * @return An instance of the given class.
     * @throws RuntimeException Thrown, if the class could not be instantiated. The exception
     *     contains a detailed message about the reason why the instantiation failed.
     */
    public static <T> T instantiate(Class<T> clazz) {
        if (clazz == null) {
            throw new NullPointerException();
        }

        // try to instantiate the class
        try {
            return clazz.newInstance();
        } catch (InstantiationException | IllegalAccessException iex) {
            // check for the common problem causes
            checkForInstantiation(clazz);

            // here we are, if non of the common causes was the problem. then the error was
            // most likely an exception in the constructor or field initialization
            throw new RuntimeException(
                    "Could not instantiate type '"
                            + clazz.getName()
                            + "' due to an unspecified exception: "
                            + iex.getMessage(),
                    iex);
        } catch (Throwable t) {
            String message = t.getMessage();
            throw new RuntimeException(
                    "Could not instantiate type '"
                            + clazz.getName()
                            + "' Most likely the constructor (or a member variable initialization) threw an exception"
                            + (message == null ? "." : ": " + message),
                    t);
        }
    }

    /**
     * Checks, whether the given class has a public nullary constructor.

View on GitHub (pinned to 2f3c205e92)

Solutions

  1. Give the class a public no-argument constructor (or provide a Factory/Builder the framework can use instead)
  2. On JDK 17+, add the required --add-opens java.base/java.lang=ALL-UNNAMED style JVM args if reflective instantiation of JDK classes is intended
  3. If the class legitimately cannot have a public constructor, instantiate it yourself and pass the instance instead of the Class

Example fix

// before
class MyUdf { MyUdf() {} } // package-private ctor -> IllegalAccessException path
// after
public class MyUdf { public MyUdf() {} }
Defensive patterns

Strategy: validation

Validate before calling

if (clazz.getConstructor() == null || !Modifier.isPublic(clazz.getConstructor().getModifiers())) {
    // fix the constructor before letting Flink instantiate
}

Try / catch

try { T t = InstantiationUtil.instantiate(clazz); } catch (RuntimeException e) { /* inspect getCause(): accessibility vs constructor failure */ }

Prevention

When it happens

Trigger: A class whose nullary constructor exists but is not public (IllegalAccessException), or instantiation blocked by a Java module does-not-open rule (IllegalAccessException/InaccessibleObjectException paths) — cases that pass the structural checks in checkForInstantiationError but still refuse newInstance.

Common situations: Third-party classes with non-public constructors; JPMS strong encapsulation on JDK 17+ refusing reflective access to unnamed-module classes; security manager remnants.

Related errors


AI-assisted analysis of apache/flink@2f3c205e92 (2026-08-14). Data as JSON: /api/errors/7db55e5f8cba789f. Report an issue: GitHub.