java-native-access/jna · error · IllegalArgumentException

Instantiation of " + type + " (Pointer) not allowed, is it p

Error message

Instantiation of " + type + " (Pointer) not allowed, is it public?"

What it means

JNA tries reflectively to construct the Structure via its (Pointer) constructor, which must be public. If it exists but is inaccessible (private, protected, package-private, or defined in a non-public class), Constructor.newInstance throws IllegalAccessException and JNA rethrows it as IllegalArgumentException asking "is it public?". This is a visibility problem in the user's Structure class definition.

Source

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

     */
    public static <T extends Structure> T newInstance(Class<T> type, Pointer init) throws IllegalArgumentException {
        try {
            Constructor<T> ctor = getPointerConstructor(type);
            if (ctor != null) {
                return ctor.newInstance(init);
            }
            // Not defined, fall back to the default
        }
        catch(SecurityException e) {
            // Might as well try the fallback
        }
        catch(InstantiationException e) {
            String msg = "Can't instantiate " + type;
            throw new IllegalArgumentException(msg, e);
        }
        catch(IllegalAccessException e) {
            String msg = "Instantiation of " + type + " (Pointer) not allowed, is it public?";
            throw new IllegalArgumentException(msg, e);
        }
        catch(InvocationTargetException e) {
            String msg = "Exception thrown while instantiating an instance of " + type;
            throw new IllegalArgumentException(msg, e);
        }
        T s = newInstance(type);
        if (init != PLACEHOLDER_MEMORY) {
            s.useMemory(init);
        }
        return s;
    }

    /**
     * Create a new Structure instance of the given type
     * @param type desired Structure type
     * @return the new instance
     * @throws IllegalArgumentException if the instantiation fails
     */

View on GitHub (pinned to d036ad9781)

Solutions

  1. Make the (Pointer) constructor public: `public MyStruct(Pointer p) { super(p); read(); }`.
  2. Declare the Structure class itself public (and static if nested), since a non-public class makes even public constructors inaccessible.
  3. If the constructor was added for JNA, keep it public but document it; alternatively remove it so JNA falls back to the public no-arg constructor.
  4. Provide both a public no-arg constructor and a public (Pointer) constructor to cover all instantiation paths.

Example fix

// before
MyStruct(Pointer p) { super(p); read(); }  // package-private
// after
public MyStruct(Pointer p) { super(p); read(); }
Defensive patterns

Strategy: validation

Validate before calling

static void requirePublicPointerCtor(Class<? extends Structure> type) {
    try {
        java.lang.reflect.Constructor<? extends Structure> c = type.getConstructor(Pointer.class);
        if (!java.lang.reflect.Modifier.isPublic(type.getModifiers()))
            throw new IllegalStateException(type + " class must be public");
    } catch (NoSuchMethodException e) {
        try { type.getConstructor(); }
        catch (NoSuchMethodException e2) { throw new IllegalStateException(type + " needs public no-arg or (Pointer) constructor"); }
    }
}

Type guard

static boolean isPubliclyConstructible(Class<?> c) {
    if (!java.lang.reflect.Modifier.isPublic(c.getModifiers())) return false;
    for (java.lang.reflect.Constructor<?> k : c.getConstructors()) {
        Class<?>[] p = k.getParameterTypes();
        if (p.length == 0 || (p.length == 1 && p[0] == Pointer.class)) return true;
    }
    return false;
}

Try / catch

try {
    MyStruct s = Structure.newInstance(MyStruct.class, ptr);
} catch (IllegalArgumentException e) {
    if (e.getMessage() != null && e.getMessage().contains("not allowed, is it public?")) {
        throw new IllegalStateException("Make " + MyStruct.class + " and its (Pointer) constructor public", e);
    }
    throw e;
}

Prevention

When it happens

Trigger: Declaring a Structure with a (Pointer) constructor marked non-public (or the class itself non-public, e.g. a package-private or inner class), then instantiating it via Structure.newInstance(type, pointer) or having JNA create it for nested/by-value layout or a native return value.

Common situations: Making the Pointer constructor package-private to 'hide' it from users; declaring the Structure as a non-static private inner class in a test; Kotlin/data-class conversions making constructors non-public; moving a Structure class out of a public file without a public modifier.

Understand the failure class

Background: Permission denied / not authorized / 403 Forbidden: access-control rejections when the caller lacks the required role, grant, or ownership — this error's family across 18 libraries.

Related errors


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