java-native-access/jna · error · UnsupportedOperationException

This VM does not support Structures with final fields (field

Error message

This VM does not support Structures with final fields (field '" + field.getName() + "' within " + getClass() + ")

What it means

Thrown when Structure.setFieldValue tries to write a final field, overrideFinal is requested, and the VM still refuses (setAccessible(true) cannot make final fields writable on some VMs, notably J2ME). JNA reports that this VM does not support Structures with final fields.

Source

Thrown at src/com/sun/jna/Structure.java:680

     * @param field field to set
     * @param value value to set
     */
    void setFieldValue(Field field, Object value) {
        setFieldValue(field, value, false);
    }

    private void setFieldValue(Field field, Object value, boolean overrideFinal) {

        try {
            field.set(this, value);
        }
        catch(IllegalAccessException e) {
            int modifiers = field.getModifiers();
            if (Modifier.isFinal(modifiers)) {
                if (overrideFinal) {
                    // WARNING: setAccessible(true) on J2ME does *not* allow
                    // overwriting of a final field.
                    throw new UnsupportedOperationException("This VM does not support Structures with final fields (field '" + field.getName() + "' within " + getClass() + ")", e);
                }
                throw new UnsupportedOperationException("Attempt to write to read-only field '" + field.getName() + "' within " + getClass(), e);
            }
            throw new Error("Unexpectedly unable to write to field '" + field.getName() + "' within " + getClass(), e);
        }
    }

    /** Only keep the original structure if its native address is unchanged.
     * Otherwise replace it with a new object.
     * @param type Structure subclass
     * @param s Original Structure object
     * @param address the native <code>struct *</code>
     * @return Updated <code>Structure.ByReference</code> object
     */
    static <T extends Structure> T updateStructureByReference(Class<T> type, T s, Pointer address) {
        if (address == null) {
            s = null;
        }

View on GitHub (pinned to d036ad9781)

Solutions

  1. Remove the final modifier from the Structure field
  2. Mark the field volatile or keep it plain so JNA can write it during read()/writeField()
  3. If the value is meant to be constant, use a static final constant outside the structure layout instead of an instance final field
  4. Run on a standard JVM (HotSpot/OpenJ9) that supports final-field override via setAccessible

Example fix

// before
public final int code;
// after
public int code;
Defensive patterns

Strategy: type-guard

Validate before calling

static boolean isFinalWritable(java.lang.reflect.Field f) {
    int m = f.getModifiers();
    return !java.lang.reflect.Modifier.isFinal(m);
}

Type guard

if (java.lang.reflect.Modifier.isFinal(field.getModifiers())) {
    throw new IllegalStateException("JNA cannot write final field on this VM: " + field.getName());
}

Try / catch

try { struct.writeField(name, value); } catch (UnsupportedOperationException e) { /* final field on this VM */ throw new IllegalArgumentException("Make field non-final: " + name, e); }

Prevention

When it happens

Trigger: Declaring a Java field final in a Structure subclass and letting JNA read native memory into it on a VM that cannot override final fields even with setAccessible(true); calling writeField(name, value) on a final field where override applies.

Common situations: Porting JNA-based code to J2ME/Embedded JVMs; code that works on HotSpot but fails on constrained VMs; legacy structures with final fields after JVM security tightening.

Understand the failure class

Background: "unsupported platform" / "not supported on this platform" errors: what they mean and how to fix them — this error's family across 47 libraries.

Related errors


AI-assisted analysis of java-native-access/jna@d036ad9781 (2026-09-12). Data as JSON: /api/errors/4e442c192e1bf7fe. Report an issue: GitHub.