java-native-access/jna · error · java.lang.IllegalArgumentException
No suitable constructor found for class:
Error message
No suitable constructor found for class:
What it means
Structure.validate(Class) ensures a Structure subclass has a public no-arg constructor, which JNA needs to reflectively instantiate the class (e.g. for auto-allocation, newInstance, nested arrays). If cls.getConstructor() throws NoSuchMethodException (or is blocked by SecurityException), it throws IllegalArgumentException with the class name.
Source
Thrown at src/com/sun/jna/Structure.java:2413
/** Placeholder pointer to help avoid auto-allocation of memory where a
* Structure needs a valid pointer but want to avoid actually reading from it.
*/
private static final Pointer PLACEHOLDER_MEMORY = new Pointer(0) {
@Override
public Pointer share(long offset, long sz) { return this; }
};
/** Indicate whether the given Structure class can be created by JNA.
* @param cls Structure subclass to check
*/
static void validate(Class<? extends Structure> cls) {
try {
cls.getConstructor();
return;
}catch(NoSuchMethodException | SecurityException e) {
}
throw new IllegalArgumentException("No suitable constructor found for class: " + cls.getName());
}
}
View on GitHub (pinned to d036ad9781)
Solutions
- Add an explicit public no-arg constructor to the Structure subclass (keep any parameterized ones as extras)
- Make the class a top-level or static nested class so its no-arg constructor is accessible without an enclosing instance
- Ensure the no-arg constructor is public and not blocked by a SecurityManager; grant reflect access or remove the restrictive policy
- Initialize fields in the no-arg constructor or via field initializers so JNA's reflective instantiation produces a valid struct
Example fix
// before
public class Point extends Structure {
public Point(int x, int y) { ... } // no no-arg constructor
}
// after
public class Point extends Structure {
public int x, y;
public Point() {}
public Point(int x, int y) { this.x = x; this.y = y; }
} Defensive patterns
Strategy: validation
Validate before calling
void validateStructureClass(Class<? extends Structure> cls) {
if (!cls.isMemberClass() || java.lang.reflect.Modifier.isStatic(cls.getModifiers())) {
try { cls.getConstructor(); }
catch (NoSuchMethodException e) {
throw new IllegalArgumentException(cls + " needs a public no-arg constructor");
}
}
} Type guard
boolean hasPublicNoArgCtor(Class<?> c) {
try { return java.lang.reflect.Modifier.isPublic(c.getConstructor().getModifiers()); }
catch (NoSuchMethodException e) { return false; }
} Try / catch
try {
MyStruct[] arr = (MyStruct[]) new MyStruct().toArray(n);
} catch (IllegalArgumentException e) {
if (e.getMessage() != null && e.getMessage().startsWith("No suitable constructor")) {
throw new IllegalStateException("Add a public no-arg constructor to " + e.getMessage());
}
throw e;
} Prevention
- Always declare a public no-arg constructor in every Structure subclass
- Make nested Structure classes static (or top-level) so no enclosing instance is required
- Invoke the class reflectively in a unit test to surface missing default constructors early
- Keep field initialization in the no-arg constructor/field initializers, not only in parameterized constructors
When it happens
Trigger: Defining a Structure subclass with only parameterized constructors, a private/protected no-arg constructor, or an anonymous/local Structure class lacking an accessible no-arg constructor, then having JNA instantiate it reflectively (toArray, nested structures, Structure.newInstance).
Common situations: Adding a convenience constructor with arguments and removing the implicit default constructor, using a SecurityManager that blocks reflective access to the class, or declaring nested Structure classes as non-static inner classes whose constructors require an enclosing instance.
Related errors
- Instantiation of " + type + " (Pointer) not allowed, is it p
- Exception thrown while instantiating an instance of " + type
- Can't create an instance of <klass>, requires a public no-ar
- This VM does not support read-only fields (field '" + field.
- Exception reading field '" + f.getName() + "' in " + getClas
AI-assisted analysis of java-native-access/jna@d036ad9781 (2026-09-12).
Data as JSON: /api/errors/d912635531db4c2c.
Report an issue: GitHub.