java-native-access/jna · error · java.lang.IllegalArgumentException
Native size for type "cls.getName()" is unknown
Error message
Native size for type "cls.getName()" is unknown
What it means
The no-value Native.getNativeSize(Class) enumerates every category JNA can size (primitives, wrappers, Structure, arrays, Pointer, Buffer, Callback, String, WString, etc.). If the class matches none, JNA cannot know how many bytes it occupies natively and throws this IllegalArgumentException. It signals an unsupported mapping, mirroring error 364 but for the type-only size lookup.
Source
Thrown at src/com/sun/jna/Native.java:1515
if (cls == char.class || cls == Character.class) return WCHAR_SIZE;
if (cls == int.class || cls == Integer.class) return 4;
if (cls == long.class || cls == Long.class) return 8;
if (cls == float.class || cls == Float.class) return 4;
if (cls == double.class || cls == Double.class) return 8;
if (Structure.class.isAssignableFrom(cls)) {
if (Structure.ByValue.class.isAssignableFrom(cls)) {
return Structure.size((Class<? extends Structure>) cls);
}
return POINTER_SIZE;
}
if (Pointer.class.isAssignableFrom(cls)
|| (Platform.HAS_BUFFERS && Buffers.isBuffer(cls))
|| Callback.class.isAssignableFrom(cls)
|| String.class == cls
|| WString.class == cls) {
return POINTER_SIZE;
}
throw new IllegalArgumentException("Native size for type \"" + cls.getName()
+ "\" is unknown");
}
/**
* @param cls The Java class
* @return {@code true} whether the given class is supported as a native argument type.
*/
public static boolean isSupportedNativeType(Class<?> cls) {
if (Structure.class.isAssignableFrom(cls)) {
return true;
}
try {
return getNativeSize(cls) != 0;
}
catch(IllegalArgumentException e) {
return false;
}
}View on GitHub (pinned to d036ad9781)
Solutions
- Only call Native.getNativeSize for JNA-supported classes; for structs use Structure.size(class) instead.
- Convert unsupported types to supported mappings (Structure, IntegerType, PointerType, arrays of primitives, String/WString).
- If you need the pointer-sized opaque value, use Pointer.class which resolves to POINTER_SIZE.
- Check Native.isSupportedNativeType(cls) (the guard right below this throw) before querying sizes.
Example fix
// before
int sz = Native.getNativeSize(MyPojo.class); // unknown native size
// after
if (Native.isSupportedNativeType(MyPojo.class)) {
int sz = Native.getNativeSize(MyPojo.class);
} else {
int sz = Structure.size(MyStruct.class); // map it as a Structure
} Defensive patterns
Strategy: validation
Validate before calling
if (!Native.isSupportedNativeType(cls)) {
throw new IllegalArgumentException("Cannot query native size for unsupported type: " + cls);
}
int size = Native.getNativeSize(cls); Type guard
static boolean hasKnownNativeSize(Class<?> cls) {
return Native.isSupportedNativeType(cls);
} Try / catch
try {
return Native.getNativeSize(cls);
} catch (IllegalArgumentException e) {
if (e.getMessage().contains("is unknown")) {
return Structure.size((Class<? extends Structure>) cls); // if it is a struct
} else { throw e; }
} Prevention
- Call Native.isSupportedNativeType before any getNativeSize(Class) query.
- Use Structure.size(...) for struct classes instead of getNativeSize.
- Keep a whitelist of supported classes in size-computation utilities.
- Add tests that iterate all mapped field types and assert support.
When it happens
Trigger: Calling Native.getNativeSize(SomeClass.class) with an unsupported class, or mapping structures/callbacks whose field/parameter types are not JNA-mappable, causing internal size queries on unknown types.
Common situations: Size-querying a custom class to plan memory allocations, using boxed generic types (List, Optional) as mapped fields, or typos where an unsupported wrapper type replaced a primitive in a Structure definition.
Understand the failure class
Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.
Related errors
- The type "type.getName()" is not supported: e.getMessage()
- Unsupported type " + cls
- Unsupported empty type:
- Unsupported type:
- Callback argument <nativeParamTypes[i]> requires custom type
AI-assisted analysis of java-native-access/jna@d036ad9781 (2026-09-12).
Data as JSON: /api/errors/e65c5f554476696f.
Report an issue: GitHub.