{"record":{"id":"24a1d2ff28e67e68","repo":"apache/flink","slug":"access-to-the-main-method-was-denied-e-getmessag","errorCode":null,"errorMessage":"Access to the main method was denied: {e.getMessage()}","messagePattern":"Access to the main method was denied: (.+?)","errorType":"exception","errorClass":"ProgramInvocationException","httpStatus":null,"severity":"error","filePath":"flink-clients/src/main/java/org/apache/flink/client/program/PackagedProgram.java","lineNumber":364,"sourceCode":"                    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) {\n                throw (ProgramInvocationException) exceptionInMethod;\n            } else {\n                throw new ProgramInvocationException(\n                        \"The main method caused an error: \" + exceptionInMethod.getMessage(),\n                        exceptionInMethod);\n            }\n        } catch (Throwable t) {\n            throw new ProgramInvocationException(\n                    \"An error occurred while invoking the program's main method: \" + t.getMessage(),\n                    t);","sourceCodeStart":346,"sourceCodeEnd":382,"githubUrl":"https://github.com/apache/flink/blob/2f3c205e9266cb30240eb7f4fdab15cad629a70f/flink-clients/src/main/java/org/apache/flink/client/program/PackagedProgram.java#L346-L382","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Read the appended ': <message>' for the exact denied package/class.","On Java 17+, add '--add-opens <module>/<package>=ALL-UNNAMED' to the JVM flags (flink-conf / FLINK_ENV_JAVA_OPTS).","If non-modular, ensure the jar is not loaded in a way that hides the package; re-export the package.","Verify the user-code classloader is the one that loaded the entry class."],"exampleFix":"# before: jar is modular without opens\nmodule my.app { exports com.example; }\n\n# after: open the package to Flink at runtime\n# FLINK_ENV_JAVA_OPTS=\"--add-opens my.app/com.example=ALL-UNNAMED\"","handlingStrategy":"validation","validationCode":"// Test reflective invoke in isolation\ntry {\n    Method m = entryClass.getMethod(\"main\", String[].class);\n    m.setAccessible(true);\n    m.invoke(null, (Object) new String[0]);\n} catch (IllegalAccessException iae) {\n    // configure --add-opens / module exports before submitting\n}","typeGuard":null,"tryCatchPattern":"try {\n    packagedProgram.invokeInteractiveModeForExecution();\n} catch (ProgramInvocationException pie) {\n    if (pie.getMessage().startsWith(\"Access to the main method was denied\")) {\n        // add --add-opens <module>/<pkg>=ALL-UNNAMED to JVM opts\n    }\n    throw pie;\n}","preventionTips":["On Java 17+, pre-configure '--add-opens' for user-code packages in FLINK_ENV_JAVA_OPTS.","Keep user jars non-modular unless you fully control module openness.","Confirm the user-code classloader visibility of the entry class."],"tags":["packaged-program","invocation","module-system","access-denied"],"backgroundTag":null,"analyzedSha":"2f3c205e9266cb30240eb7f4fdab15cad629a70f","analyzedAt":"2026-08-14T08:48:24.518Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}