{"record":{"id":"2a1ca88d17fb3a44","repo":"java-native-access/jna","slug":"class-cls-getname-is-not-currently-registered","errorCode":null,"errorMessage":"Class cls.getName() is not currently registered","messagePattern":"Class cls\\.getName\\(\\) is not currently registered","errorType":"exception","errorClass":"java.lang.IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/com/sun/jna/Native.java","lineNumber":2067,"sourceCode":"\n    /**\n     * Get the {@link NativeLibrary} instance to which the given \"registered\"\n     * class is bound.\n     *\n     * @param cls the \"registered\" class, which was previously registered via\n     * the {@link Native#register register()} method\n     * @return the {@link NativeLibrary} instance to which the \"registered\"\n     * class is bound\n     */\n    public static NativeLibrary getNativeLibrary(final Class<?> cls) {\n        if(cls == null) {\n            throw new IllegalArgumentException(\"null passed to getNativeLibrary\");\n        }\n        final Class<?> mappedClass = findDirectMappedClass(cls);\n        synchronized(registeredClasses) {\n            final NativeLibrary nativeLibrary = registeredLibraries.get(mappedClass);\n            if (nativeLibrary == null) {\n                throw new IllegalArgumentException(\"Class \" + cls.getName() + \" is not currently registered\");\n            } else {\n                return nativeLibrary;\n            }\n        }\n    }\n\n    /* Take note of options used for a given library mapping, to facilitate\n     * looking them up later.\n     */\n    private static Map<String, Object> cacheOptions(Class<?> cls, Map<String, ?> options, Object proxy) {\n        Map<String, Object> libOptions = new HashMap<>(options);\n        libOptions.put(_OPTION_ENCLOSING_LIBRARY, cls);\n        typeOptions.put(cls, libOptions);\n        if (proxy != null) {\n            libraries.put(cls, new WeakReference<>(proxy));\n        }\n\n        // If it's a direct mapping, AND implements a Library interface,","sourceCodeStart":2049,"sourceCodeEnd":2085,"githubUrl":"https://github.com/java-native-access/jna/blob/d036ad9781adad4b66693e8fa7098e4ac665e0a3/src/com/sun/jna/Native.java#L2049-L2085","documentation":"This IllegalArgumentException means the class (or its direct-mapped equivalent) has no entry in JNA's registeredLibraries map — Native.register() was never called for it, or registration happened in a different classloader/session. The registry is keyed by the class findDirectMappedClass(cls) returns, so a mismatch there also lands here.","triggerScenarios":"Calling Native.getNativeLibrary(SomeClass.class) before Native.register(SomeClass.class, lib) ran; querying after a failed registration; calling from a different ClassLoader (app servers, plugin systems); passing a class that is a companion of, but not itself, the registered one.","commonSituations":"Static initialization order bugs; OSGi/webapp reload losing the registration; assuming register() side effects from a different library version.","solutions":["Call Native.register(cls, nativeLibrary) before querying getNativeLibrary.","Use the same ClassLoader for registration and lookup; deduplicate JNA on the classpath.","Verify findDirectMappedClass matches: query with the exact class that was registered.","Check that registration did not throw earlier and fail silently."],"exampleFix":"// before\nNativeLibrary nl = Native.getNativeLibrary(MyNative.class); // not yet registered\n// after\nNative.register(MyNative.class, NativeLibrary.getInstance(\"mylib\"));\nNativeLibrary nl = Native.getNativeLibrary(MyNative.class);","handlingStrategy":"validation","validationCode":"synchronized (Native.class) {\n    if (!isRegistered) {\n        Native.register(MyNative.class, NativeLibrary.getInstance(\"mylib\"));\n        isRegistered = true;\n    }\n}\nNativeLibrary nl = Native.getNativeLibrary(MyNative.class);","typeGuard":null,"tryCatchPattern":"try {\n    NativeLibrary nl = Native.getNativeLibrary(MyNative.class);\n} catch (IllegalArgumentException e) {\n    if (e.getMessage() != null && e.getMessage().endsWith(\"is not currently registered\")) {\n        ensureRegistered(); // idempotent register()\n        nl = Native.getNativeLibrary(MyNative.class);\n    } else {\n        throw e;\n    }\n}","preventionTips":["Register classes in a static initializer or explicit init() that always runs before use.","In app servers/OSGi, register and look up within the same classloader.","Make registration idempotent and call it lazily on first access.","Log registration success so failures are visible."],"tags":["jna","registration","not-registered","lifecycle"],"backgroundTag":"entity-not-found","analyzedSha":"d036ad9781adad4b66693e8fa7098e4ac665e0a3","analyzedAt":"2026-09-12T06:50:59.239Z","contentChangedAt":"2026-09-12T06:50:59.239Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}