apache/flink · error · ProgramInvocationException

The class %s declares a non-static main method.

Error message

The class %s declares a non-static main method.

What it means

Thrown by callMainMethod after the main method is found, when Modifier.isStatic returns false. A non-static main method cannot be invoked without an instance and is not a valid JVM entry point.

Source

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

                    "The class " + entryClass.getName() + " must be public.");
        }

        try {
            mainMethod = entryClass.getMethod("main", String[].class);
        } catch (NoSuchMethodException e) {
            throw new ProgramInvocationException(
                    "The class " + entryClass.getName() + " has no main(String[]) method.");
        } catch (Throwable t) {
            throw new ProgramInvocationException(
                    "Could not look up the main(String[]) method from the class "
                            + entryClass.getName()
                            + ": "
                            + t.getMessage(),
                    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) {

View on GitHub (pinned to 2f3c205e92)

Solutions

  1. Add the 'static' modifier to the main method: 'public static void main(String[] args)'.
  2. Rebuild and resubmit the jar.

Example fix

// before
public class MyJob {
    public void main(String[] args) { ... } // missing static
}

// after
public class MyJob {
    public static void main(String[] args) throws Exception { ... }
}
Defensive patterns

Strategy: validation

Validate before calling

try {
    Method m = entryClass.getMethod("main", String[].class);
    if (!Modifier.isStatic(m.getModifiers())) {
        throw new IllegalArgumentException("main must be static");
    }
} catch (NoSuchMethodException e) {
    throw new IllegalArgumentException("no main(String[])", e);
}

Try / catch

try {
    packagedProgram.invokeInteractiveModeForExecution();
} catch (ProgramInvocationException pie) {
    if (pie.getMessage().contains("non-static main")) {
        // add 'static' to main and rebuild
    }
    throw pie;
}

Prevention

When it happens

Trigger: The class declares a main(String[]) method but omits the 'static' keyword.

Common situations: Copy-paste of a method body into a new class without the static modifier; refactoring that removed 'static'; submitting a class meant to be instantiated rather than run directly.

Related errors


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