{"record":{"id":"75cac4d9acde3a7d","repo":"java-native-access/jna","slug":"jna-can-t-attach-native-thread-to-vm-on-load","errorCode":null,"errorMessage":"JNA: Can't attach native thread to VM on load\n","messagePattern":"JNA: Can't attach native thread to VM on load\n","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"native/dispatch.c","lineNumber":3358,"sourceCode":"\nJNIEXPORT jstring JNICALL\nJava_com_sun_jna_Native_getAPIChecksum(JNIEnv *env, jclass UNUSED(classp)) {\n#ifndef CHECKSUM\n#define CHECKSUM \"undefined\"\n#endif\n  return newJavaString(env, CHECKSUM, CHARSET_UTF8);\n}\n\nJNIEXPORT jint JNICALL\nJNI_OnLoad(JavaVM *jvm, void *UNUSED(reserved)) {\n  JNIEnv* env;\n  int result = JNI_VERSION_1_4;\n  int attached = (*jvm)->GetEnv(jvm, (void *)&env, JNI_VERSION_1_4) == JNI_OK;\n  const char* err;\n\n  if (!attached) {\n    if ((*jvm)->AttachCurrentThread(jvm, (void *)&env, NULL) != JNI_OK) {\n      fprintf(stderr, \"JNA: Can't attach native thread to VM on load\\n\");\n      return 0;\n    }\n  }\n\n  if ((err = JNA_init(env)) != NULL) {\n    fprintf(stderr, \"JNA: Problems loading core IDs: %s\\n\", err);\n    result = 0;\n  }\n  else if ((err = JNA_callback_init(env)) != NULL) {\n    fprintf(stderr, \"JNA: Problems loading callback IDs: %s\\n\", err);\n    result = 0;\n  }\n  if (!attached) {\n    if ((*jvm)->DetachCurrentThread(jvm) != 0) {\n      fprintf(stderr, \"JNA: could not detach thread on initial load\\n\");\n    }\n  }\n","sourceCodeStart":3340,"sourceCodeEnd":3376,"githubUrl":"https://github.com/java-native-access/jna/blob/d036ad9781adad4b66693e8fa7098e4ac665e0a3/native/dispatch.c#L3340-L3376","documentation":"In the JNA native library's load/initialization entry point, if the loading thread is not already attached to the JVM, JNA calls AttachCurrentThread to obtain a JNIEnv for initialization. If attachment fails, it prints this message and returns 0, failing the native library load — subsequent JNA operations will fail because core IDs and method IDs were never initialized.","triggerScenarios":"JNI_OnLoad / JNA native init runs on a thread the JVM cannot attach (VM already destroying, or a host process loading the library outside a proper JVM lifecycle), so (*jvm)->AttachCurrentThread returns != JNI_OK.","commonSituations":"Library loaded too late during shutdown; embedding JNA's native lib in a non-JVM process; version mismatches where the native dispatch library is loaded by a different classloader/VM instance than expected.","solutions":["Load the JNA native library early in a healthy JVM (touch a JNA class on the main thread at startup) rather than lazily during teardown.","Verify only one JVM instance exists in the process and the native library is not shared across JVMs.","Check that no shutdown hooks or finalizers trigger JNA loading after the VM begins destruction.","If the load fails, restart the JVM cleanly; subsequent JNA calls cannot work with uninitialized core IDs."],"exampleFix":"// before: first JNA touch inside a shutdown hook\nRuntime.getRuntime().addShutdownHook(new Thread(() -> Native.getLastError()));\n// after: initialize JNA at startup\npublic static void main(String[] a) {\n  Native.register(NativeLibrary.class); // force early native load\n  ...\n}","handlingStrategy":"validation","validationCode":"// Force JNA native init early, on the main thread, while the JVM is healthy:\nstatic { com.sun.jna.Native.class.getName(); /* touch JNA before any shutdown path */ }","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Initialize JNA at application startup, never inside shutdown hooks or finalizers.","Run a single JVM per process; do not share the JNA native library across VM instances.","Include a startup smoke test that performs a trivial JNA native call."],"tags":["jni","jna","native","initialization","threads","attach"],"backgroundTag":"jni-thread-attach-failed","analyzedSha":"d036ad9781adad4b66693e8fa7098e4ac665e0a3","analyzedAt":"2026-09-12T06:50:59.239Z","contentChangedAt":"2026-09-12T06:50:59.239Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}