{"record":{"id":"19c3a77887b0ebca","repo":"apache/flink","slug":"the-class-s-declares-a-non-public-main-method","errorCode":null,"errorMessage":"The class %s declares a non-public main method.","messagePattern":"The class (.+?) declares a non-public main method\\.","errorType":"exception","errorClass":"ProgramInvocationException","httpStatus":null,"severity":"error","filePath":"flink-clients/src/main/java/org/apache/flink/client/program/PackagedProgram.java","lineNumber":354,"sourceCode":"            mainMethod = entryClass.getMethod(\"main\", String[].class);\n        } catch (NoSuchMethodException e) {\n            throw new ProgramInvocationException(\n                    \"The class \" + entryClass.getName() + \" has no main(String[]) method.\");\n        } catch (Throwable t) {\n            throw new ProgramInvocationException(\n                    \"Could not look up the main(String[]) method from the class \"\n                            + entryClass.getName()\n                            + \": \"\n                            + t.getMessage(),\n                    t);\n        }\n\n        if (!Modifier.isStatic(mainMethod.getModifiers())) {\n            throw new ProgramInvocationException(\n                    \"The class \" + entryClass.getName() + \" declares a non-static main method.\");\n        }\n        if (!Modifier.isPublic(mainMethod.getModifiers())) {\n            throw new ProgramInvocationException(\n                    \"The class \" + entryClass.getName() + \" declares a non-public main method.\");\n        }\n\n        try {\n            mainMethod.invoke(null, (Object) args);\n        } catch (IllegalArgumentException e) {\n            throw new ProgramInvocationException(\n                    \"Could not invoke the main method, arguments are not matching.\", e);\n        } catch (IllegalAccessException e) {\n            throw new ProgramInvocationException(\n                    \"Access to the main method was denied: \" + e.getMessage(), e);\n        } catch (InvocationTargetException e) {\n            Throwable exceptionInMethod = e.getTargetException();\n            if (exceptionInMethod instanceof Error) {\n                throw (Error) exceptionInMethod;\n            } else if (exceptionInMethod instanceof ProgramParametrizationException) {\n                throw (ProgramParametrizationException) exceptionInMethod;\n            } else if (exceptionInMethod instanceof ProgramInvocationException) {","sourceCodeStart":336,"sourceCodeEnd":372,"githubUrl":"https://github.com/apache/flink/blob/2f3c205e9266cb30240eb7f4fdab15cad629a70f/flink-clients/src/main/java/org/apache/flink/client/program/PackagedProgram.java#L336-L372","documentation":"Thrown by callMainMethod when the located main method is static but Modifier.isPublic returns false. A non-public main method (even if static) cannot be reflectively invoked across classloader boundaries in Flink's user-code isolation model.","triggerScenarios":"The class declares 'static void main(String[])' but without 'public' (package-private or protected).","commonSituations":"Omitting 'public' on main, common in internal utility classes. Flink requires public so the user-code classloader can reflectively invoke it.","solutions":["Add the 'public' modifier: 'public static void main(String[] args)'.","Rebuild and resubmit."],"exampleFix":"// before\npublic class MyJob {\n    static void main(String[] args) { ... } // not public\n}\n\n// after\npublic class MyJob {\n    public static void main(String[] args) throws Exception { ... }\n}","handlingStrategy":"validation","validationCode":"try {\n    Method m = entryClass.getMethod(\"main\", String[].class);\n    if (!Modifier.isPublic(m.getModifiers())) {\n        throw new IllegalArgumentException(\"main must be public\");\n    }\n} catch (NoSuchMethodException e) {\n    throw new IllegalArgumentException(\"no main(String[])\", e);\n}","typeGuard":null,"tryCatchPattern":"try {\n    packagedProgram.invokeInteractiveModeForExecution();\n} catch (ProgramInvocationException pie) {\n    if (pie.getMessage().contains(\"non-public main\")) {\n        // add 'public' to main and rebuild\n    }\n    throw pie;\n}","preventionTips":["Always use 'public static void main(String[] args)'.","Lint the entry class with a build-time check that main is public."],"tags":["packaged-program","main-method","access-modifiers"],"backgroundTag":null,"analyzedSha":"2f3c205e9266cb30240eb7f4fdab15cad629a70f","analyzedAt":"2026-08-14T08:48:24.518Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}