apache/flink · error · ProgramInvocationException

Access to the main method was denied: {e.getMessage()}

Error message

Access to the main method was denied: {e.getMessage()}

What it means

Thrown by callMainMethod when Method.invoke raises IllegalAccessException. Because the code already verified the method is public and static, this typically indicates a module-access (JPMS) restriction or a classloader visibility issue rather than a plain modifier problem.

Source

Thrown at flink-clients/src/main/java/org/apache/flink/client/program/PackagedProgram.java:364

                    t);
        }

        if (!Modifier.isStatic(mainMethod.getModifiers())) {
            throw new ProgramInvocationException(
                    "The class " + entryClass.getName() + " declares a non-static main method.");
        }
        if (!Modifier.isPublic(mainMethod.getModifiers())) {
            throw new ProgramInvocationException(
                    "The class " + entryClass.getName() + " declares a non-public main method.");
        }

        try {
            mainMethod.invoke(null, (Object) args);
        } catch (IllegalArgumentException e) {
            throw new ProgramInvocationException(
                    "Could not invoke the main method, arguments are not matching.", e);
        } catch (IllegalAccessException e) {
            throw new ProgramInvocationException(
                    "Access to the main method was denied: " + e.getMessage(), e);
        } catch (InvocationTargetException e) {
            Throwable exceptionInMethod = e.getTargetException();
            if (exceptionInMethod instanceof Error) {
                throw (Error) exceptionInMethod;
            } else if (exceptionInMethod instanceof ProgramParametrizationException) {
                throw (ProgramParametrizationException) exceptionInMethod;
            } else if (exceptionInMethod instanceof ProgramInvocationException) {
                throw (ProgramInvocationException) exceptionInMethod;
            } else {
                throw new ProgramInvocationException(
                        "The main method caused an error: " + exceptionInMethod.getMessage(),
                        exceptionInMethod);
            }
        } catch (Throwable t) {
            throw new ProgramInvocationException(
                    "An error occurred while invoking the program's main method: " + t.getMessage(),
                    t);

View on GitHub (pinned to 2f3c205e92)

Solutions

  1. Read the appended ': <message>' for the exact denied package/class.
  2. On Java 17+, add '--add-opens <module>/<package>=ALL-UNNAMED' to the JVM flags (flink-conf / FLINK_ENV_JAVA_OPTS).
  3. If non-modular, ensure the jar is not loaded in a way that hides the package; re-export the package.
  4. Verify the user-code classloader is the one that loaded the entry class.

Example fix

# before: jar is modular without opens
module my.app { exports com.example; }

# after: open the package to Flink at runtime
# FLINK_ENV_JAVA_OPTS="--add-opens my.app/com.example=ALL-UNNAMED"
Defensive patterns

Strategy: validation

Validate before calling

// Test reflective invoke in isolation
try {
    Method m = entryClass.getMethod("main", String[].class);
    m.setAccessible(true);
    m.invoke(null, (Object) new String[0]);
} catch (IllegalAccessException iae) {
    // configure --add-opens / module exports before submitting
}

Try / catch

try {
    packagedProgram.invokeInteractiveModeForExecution();
} catch (ProgramInvocationException pie) {
    if (pie.getMessage().startsWith("Access to the main method was denied")) {
        // add --add-opens <module>/<pkg>=ALL-UNNAMED to JVM opts
    }
    throw pie;
}

Prevention

When it happens

Trigger: Java 9+ module system denies access to a public method because the declaring module does not open/export the package to Flink, or the user-code classloader cannot see the class.

Common situations: Running modular user code on Java 17+ without '--add-opens'; classloader hierarchy problems where the entry class is loaded by a parent loader not visible to the invoking loader.

Related errors


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