{"record":{"id":"8d2a9504025429bf","repo":"openjdk/jdk","slug":"failed-to-find-premain-class-manifest-attribute-in","errorCode":null,"errorMessage":"Failed to find Premain-Class manifest attribute in %s\\n","messagePattern":"Failed to find Premain-Class manifest attribute in (.+?)\\\\n","errorType":"console","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"src/java.instrument/share/native/libinstrument/InvocationAdapter.c","lineNumber":187,"sourceCode":"         * Agent_OnLoad is specified to provide the agent options\n         * argument tail in modified UTF8. However for 1.5.0 this is\n         * actually in the platform encoding - see 5049313.\n         *\n         * Open zip/jar file and parse archive. If can't be opened or\n         * not a zip file return error. Also if Premain-Class attribute\n         * isn't present we return an error.\n         */\n        attributes = readAttributes(jarfile);\n        if (attributes == NULL) {\n            fprintf(stderr, \"Error opening zip file or JAR manifest missing : %s\\n\", jarfile);\n            free(jarfile);\n            if (options != NULL) free(options);\n            return JNI_ERR;\n        }\n\n        premainClass = getAttribute(attributes, \"Premain-Class\");\n        if (premainClass == NULL) {\n            fprintf(stderr, \"Failed to find Premain-Class manifest attribute in %s\\n\",\n                jarfile);\n            free(jarfile);\n            if (options != NULL) free(options);\n            freeAttributes(attributes);\n            return JNI_ERR;\n        }\n\n        /*\n         * The value of the Premain-Class attribute becomes the agent\n         * class name. The manifest is in UTF8 so need to convert to\n         * modified UTF8 (see JNI spec).\n         */\n        oldLen = (int)strlen(premainClass);\n        newLen = modifiedUtf8LengthOfUtf8(premainClass, oldLen);\n        /*\n         * According to JVMS class name is represented as CONSTANT_Utf8_info,\n         * so its length is u2 (i.e. must be <= 0xFFFF).\n         * Negative oldLen or newLen means we got signed integer overflow","sourceCodeStart":169,"sourceCodeEnd":205,"githubUrl":"https://github.com/openjdk/jdk/blob/88dfb74bbeefcf2b0aa11835183bcd949998fc8f/src/java.instrument/share/native/libinstrument/InvocationAdapter.c#L169-L205","documentation":"The agent jar opened fine and its manifest attributes were read, but the required 'Premain-Class' attribute is absent from META-INF/MANIFEST.MF. The java.lang.instrument contract requires every -javaagent jar to name its premain entry point this way. Without it the launcher prints this error, frees resources, and returns JNI_ERR, refusing to start.","triggerScenarios":"Manifest exists (so error 134 passed) but lacks the line 'Premain-Class: com.example.AgentClass'. Typical when the manifest was generated by a build plugin default template, or hand-written with a typo ('Premain class', trailing spaces in the attribute name are significant — the spec requires exact 'Premain-Class:').","commonSituations":"Maven/Gradle builds where the manifest section is added to the wrong task or the attribute is inside a nested section; multi-line folding breaking the attribute; agents built by tutorials missing the attribute entirely; using Agent-Class (for attach) only, then launching with -javaagent which requires Premain-Class.","solutions":["Add 'Premain-Class: your.Agent' to the manifest and ensure the attribute name is spelled exactly (case-sensitive, colon+space)","Configure the build: Maven jar plugin <manifestEntries><Premain-Class>...</Premain-Class></manifestEntries> or Gradle jar { manifest { attributes 'Premain-Class': 'your.Agent' } }","If you intend runtime attach instead of -javaagent, use Agent-Class and attach via the Attach API; do not launch with -javaagent","Inspect with 'unzip -p agent.jar META-INF/MANIFEST.MF' to confirm the attribute and correct section (must be in main section, before first blank line)"],"exampleFix":"// build.gradle — before\njar { manifest { attributes 'Main-Class': 'myapp.Main' } } // no Premain-Class\n\n// after\njar {\n    manifest {\n        attributes 'Premain-Class': 'com.example.MyAgent',\n                   'Can-Retransform-Classes': 'true'\n    }\n}","handlingStrategy":"validation","validationCode":"// build-time assertion that Premain-Class exists\ntry (ZipFile z = new ZipFile(\"agent.jar\")) {\n    Manifest mf = new Manifest(z.getInputStream(z.getEntry(\"META-INF/MANIFEST.MF\")));\n    String pc = mf.getMainAttributes().getValue(\"Premain-Class\");\n    if (pc == null || pc.isBlank()) throw new IllegalStateException(\"Premain-Class missing\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Declare Premain-Class in the jar manifest attributes of the build (Maven manifestEntries / Gradle attributes)","Attribute names are case-sensitive: exactly 'Premain-Class'","For attach-only agents use Agent-Class and the Attach API, not -javaagent"],"tags":["javaagent","instrumentation","manifest","premain-class","jvm-startup"],"backgroundTag":null,"analyzedSha":"88dfb74bbeefcf2b0aa11835183bcd949998fc8f","analyzedAt":"2026-08-14T11:45:09.665Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}