{"record":{"id":"e803771245311571","repo":"alibaba/arthas","slug":"error-arthas-vmtool-unable-to-create-jvmtienv-ge","errorCode":null,"errorMessage":"ERROR: arthas vmtool Unable to create jvmtiEnv, GetEnv failed, error=%d\n","messagePattern":"ERROR: arthas vmtool Unable to create jvmtiEnv, GetEnv failed, error=(.+?)\n","errorType":"console","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"arthas-vmtool/src/main/native/src/jni-library.cpp","lineNumber":46,"sourceCode":"\n    bool allow() {\n        if (limitValue < 0) {\n            return true;\n        }\n        return limitValue > currentCounter;\n    }\n};\n\n// 每次 IterateOverInstancesOfClass 调用前需要先 init\nstatic LimitCounter limitCounter = {0, 0};\n\nextern \"C\"\nint init_agent(JavaVM *vm, void *reserved) {\n    jint rc;\n    /* Get JVMTI environment */\n    rc = vm->GetEnv((void **)&jvmti, JVMTI_VERSION_1_2);\n    if (rc != JNI_OK) {\n        fprintf(stderr, \"ERROR: arthas vmtool Unable to create jvmtiEnv, GetEnv failed, error=%d\\n\", rc);\n        return -1;\n    }\n\n    jvmtiCapabilities capabilities = {0};\n    capabilities.can_tag_objects = 1;\n    jvmtiError error = jvmti->AddCapabilities(&capabilities);\n    if (error) {\n        fprintf(stderr, \"ERROR: arthas vmtool JVMTI AddCapabilities failed!%u\\n\", error);\n        return JNI_FALSE;\n    }\n\n    return JNI_OK;\n}\n\nextern \"C\" JNIEXPORT jint JNICALL\nAgent_OnLoad(JavaVM *vm, char *options, void *reserved) {\n    return init_agent(vm, reserved);\n}","sourceCodeStart":28,"sourceCodeEnd":64,"githubUrl":"https://github.com/alibaba/arthas/blob/21cf2e9ba52b305290be7223b980ff504bb9cb5b/arthas-vmtool/src/main/native/src/jni-library.cpp#L28-L64","documentation":"In the arthas-vmtool native agent (jni-library.cpp), init_agent() calls JavaVM::GetEnv() to obtain a JVMTI 1.2 environment. If GetEnv returns anything other than JNI_OK (e.g. JNI_EDETACHED, JNI_EVERSION), the agent cannot function and prints this error to stderr, returning -1. Without a JVMTI env, the agent cannot tag objects or iterate the heap.","triggerScenarios":"The vmtool agent is loaded/initialized in a context where the JVM cannot provide a JVMTI 1.2 environment: the calling thread is not attached to the JVM (JNI_EDETACHED), the JVM doesn't support the requested JVMTI version (JNI_EVERSION), or the VM is in a state that rejects GetEnv.","commonSituations":"Agent loaded via Agent_OnAttach from a thread not attached to the JVM; running on a JVM that lacks or disables JVMTI (some minimal/embedded JVMs); JVM version mismatch where JVMTI_VERSION_1_2 is unsupported; the agent was loaded too early or too late in the VM lifecycle.","solutions":["Ensure the agent is loaded through the supported Arthas attach mechanism which handles thread attachment.","Verify you are running on a standard JDK (HotSpot/OpenJ9) that provides JVMTI 1.2.","Check the JVM version and that JVMTI is not disabled by JVM flags.","If attaching programmatically, ensure the attaching thread is attached via AttachCurrentThread before loading the agent."],"exampleFix":"// not a source-code fix — environment/JVM fix\n// before: agent loaded on detached thread or unsupported JVM -> error rc=-2 (JNI_EDETACHED)\n\n// after: attach thread first, or use arthas standard attach\n// arthas-boot.jar handles attach correctly:\njava -jar arthas-boot.jar <pid>","handlingStrategy":"fallback","validationCode":"// Not fixable in application code — verify JVM/JVMTI availability\nString jvmName = System.getProperty(\"java.vm.name\");\nif (!jvmName.contains(\"HotSpot\") && !jvmName.contains(\"OpenJDK\") && !jvmName.contains(\"J9\")) {\n    log.warn(\"vmtool may not be supported on JVM: \" + jvmName);\n}","typeGuard":null,"tryCatchPattern":"// Native error — no Java try-catch. Handle by checking stderr output\n// or disabling vmtool features when the agent fails to init.\nif (!vmtoolAgentAvailable()) {\n    log.warn(\"vmtool JVMTI init failed — heap scanning disabled\");\n}","preventionTips":["Run Arthas vmtool on a standard JDK with JVMTI support.","Use the standard arthas-boot.jar attach mechanism which handles thread attachment correctly."],"tags":["arthas","vmtool","jvmti","native","jni","agent"],"backgroundTag":null,"analyzedSha":"21cf2e9ba52b305290be7223b980ff504bb9cb5b","analyzedAt":"2026-08-14T00:57:07.243Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}