{"record":{"id":"a5d96fb30a79f95c","repo":"apache/dolphinscheduler","slug":"get-mainjarname-failed","errorCode":null,"errorMessage":"get mainJarName failed:","messagePattern":"get mainJarName failed:","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"dolphinscheduler-task-plugin/dolphinscheduler-task-java/src/main/java/org/apache/dolphinscheduler/plugin/task/java/MainClassExtractor.java","lineNumber":39,"sourceCode":"import java.util.jar.JarFile;\nimport java.util.jar.Manifest;\n\nimport lombok.extern.slf4j.Slf4j;\n\n@Slf4j\npublic class MainClassExtractor {\n\n    private MainClassExtractor() {\n    }\n    public static String getMainClassName(String jarFilePath) {\n        String mainClassName = null;\n        try (JarFile jarFile = new JarFile(new File(jarFilePath))) {\n\n            Manifest manifest = jarFile.getManifest();\n            mainClassName = manifest.getMainAttributes().getValue(\"Main-Class\");\n\n        } catch (Exception e) {\n            throw new RuntimeException(\"get mainJarName failed:\", e);\n        }\n        return mainClassName;\n    }\n}\n","sourceCodeStart":21,"sourceCodeEnd":44,"githubUrl":"https://github.com/apache/dolphinscheduler/blob/02eac45a1b6676e639fcbfb4be2243de5771b05d/dolphinscheduler-task-plugin/dolphinscheduler-task-java/src/main/java/org/apache/dolphinscheduler/plugin/task/java/MainClassExtractor.java#L21-L44","documentation":"MainClassExtractor.getMainClassName opens the task's jar as a JarFile and reads the Main-Class attribute from META-INF/MANIFEST.MF. Any failure — unreadable jar, corrupt file, missing manifest, or IO error — is rethrown as a RuntimeException with message 'get mainJarName failed:'.","triggerScenarios":"getMainClassName(jarFilePath) is called and new JarFile(...) throws (file missing, not a valid zip/jar, permission denied) or jarFile.getManifest()/getMainAttributes() returns null causing NPE, or reading attributes throws.","commonSituations":"User selects a non-jar file (e.g. .zip, .class) as the main jar, jar built without Main-Class manifest attribute (no maven-jar-plugin mainClass config), resource not yet downloaded to the worker so the path is empty, or jar is corrupted on upload.","solutions":["Verify the file is a valid, executable jar: run 'unzip -p your.jar META-INF/MANIFEST.MF' and confirm a Main-Class entry.","If Main-Class is missing, rebuild with maven-jar-plugin <mainClass> configured or provide the main class explicitly in the task params.","Check the jar path exists on the worker (resource downloaded) before extraction.","Re-upload the jar if it is corrupted.","Wrap with a null check on getManifest() to produce a clearer error."],"exampleFix":"// before\nManifest manifest = jarFile.getManifest();\nmainClassName = manifest.getMainAttributes().getValue(\"Main-Class\");\n// after\nManifest manifest = jarFile.getManifest();\nif (manifest == null || manifest.getMainAttributes().getValue(\"Main-Class\") == null) {\n    throw new RuntimeException(\"jar has no Main-Class manifest attribute: \" + jarFilePath);\n}\nmainClassName = manifest.getMainAttributes().getValue(\"Main-Class\");","handlingStrategy":"validation","validationCode":"static void validateExecutableJar(String jarFilePath) throws IOException {\n    File f = new File(jarFilePath);\n    if (!f.isFile()) throw new FileNotFoundException(jarFilePath);\n    try (JarFile jf = new JarFile(f)) {\n        Manifest m = jf.getManifest();\n        if (m == null || m.getMainAttributes().getValue(\"Main-Class\") == null) {\n            throw new IllegalStateException(\"no Main-Class in \" + jarFilePath);\n        }\n    }\n}","typeGuard":"static boolean hasMainClassAttribute(String jarPath) {\n    try (JarFile jf = new JarFile(jarPath)) {\n        Manifest m = jf.getManifest();\n        return m != null && m.getMainAttributes().getValue(\"Main-Class\") != null;\n    } catch (Exception e) { return false; }\n}","tryCatchPattern":"try {\n    String mainClass = MainClassExtractor.getMainClassName(jarPath);\n} catch (RuntimeException e) {\n    log.error(\"cannot extract Main-Class from {}\", jarPath, e.getCause());\n    // fall back to explicit main class in task params\n}","preventionTips":["Always build runnable jars with the maven-jar-plugin mainClass (or shade plugin) set.","Use 'jar tf' / 'unzip -p ... META-INF/MANIFEST.MF' to verify before uploading.","Ensure the resource is fully uploaded and downloaded before the task runs.","Only select .jar files (not .zip) as main jar in the UI."],"tags":["java-task","jar","manifest","reflection"],"backgroundTag":"manifest-attribute-missing","analyzedSha":"02eac45a1b6676e639fcbfb4be2243de5771b05d","analyzedAt":"2026-09-06T17:43:00.555Z","contentChangedAt":"2026-09-06T17:43:00.555Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}