java-native-access/jna · error · IllegalArgumentException
Can't create an instance of <klass>, requires a public no-ar
Error message
Can't create an instance of <klass>, requires a public no-arg constructor: <cause>
What it means
Klass.newInstance wraps reflection failures (IllegalAccess, NoSuchMethod, InstantiationException, etc.) into this IllegalArgumentException when it cannot instantiate the class via its public no-arg constructor. JNA uses it internally to create instances of Structure, Callback and similar types.
Source
Thrown at src/com/sun/jna/Klass.java:52
/**
* Create a new instance for the given {@code klass}. Runtime exceptions
* thrown from the constructor are rethrown, all other exceptions
* generated from the reflective call are wrapped into a
* {@link java.lang.IllegalArgumentException} and rethrown.
*
* @param klass desired class to instantiate
* @return the new instance
* @throws IllegalArgumentException if the instantiation fails
* @throws RuntimeException if the constructor for {@code klass} throws
* a runtime exception
*/
public static <T> T newInstance(Class<T> klass) {
try {
return klass.getDeclaredConstructor().newInstance();
} catch (IllegalAccessException | IllegalArgumentException | InstantiationException | NoSuchMethodException | SecurityException e) {
String msg = "Can't create an instance of " + klass
+ ", requires a public no-arg constructor: " + e;
throw new IllegalArgumentException(msg, e);
} catch (InvocationTargetException e) {
if (e.getCause() instanceof RuntimeException) {
throw (RuntimeException) e.getCause();
} else {
String msg = "Can't create an instance of " + klass
+ ", requires a public no-arg constructor: " + e;
throw new IllegalArgumentException(msg, e);
}
}
}
}
View on GitHub (pinned to d036ad9781)
Solutions
- Add a public no-arg constructor to the class (make it static if nested).
- Use the overload that accepts a preallocated instance, e.g. Structure's use of newInstance with a target object.
- If the class is abstract or an interface, instantiate a concrete subclass instead.
- Check the wrapped cause `e` in the message for the exact reflection failure.
Example fix
// before
class Point { Point(int x, int y) {} }
// after
class Point { public Point() {} public Point(int x, int y) {} } Defensive patterns
Strategy: validation
Validate before calling
static boolean hasPublicNoArgCtor(Class<?> k) {
try { k.getDeclaredConstructor(); return java.lang.reflect.Modifier.isPublic(k.getModifiers()) && !java.lang.reflect.Modifier.isAbstract(k.getModifiers()); }
catch (NoSuchMethodException e) { return false; }
} Type guard
static <T> T safeNewInstance(Class<T> k) {
if (!hasPublicNoArgCtor(k)) throw new IllegalArgumentException(k + " lacks a public no-arg ctor");
return com.sun.jna.Klass.newInstance(k);
} Try / catch
try {
T t = Klass.newInstance(klass);
} catch (IllegalArgumentException e) {
if (e.getMessage().contains("requires a public no-arg constructor")) {
throw new IllegalStateException("Make " + klass.getSimpleName() + " static/concrete with a public no-arg ctor", e.getCause());
}
throw e;
} Prevention
- Give Structure/mapped classes a public no-arg constructor.
- Make nested mapping classes static.
- Never map abstract classes or interfaces for instantiation.
- Read the cause appended to the message to pinpoint the reflection failure.
When it happens
Trigger: Calling Klass.newInstance(klass) on a class with no declared no-arg constructor, a non-public no-arg constructor, an abstract class, or an interface; a SecurityException from the SecurityManager.
Common situations: Nested (non-static inner) classes whose implicit constructor needs an enclosing instance; classes with only parameterized constructors; anonymous/local classes; instantiating interfaces by mistake.
Related errors
- Can't instantiate " + type
- Instantiation of " + type + " (Pointer) not allowed, is it p
- Exception thrown while instantiating an instance of " + type
- No suitable constructor found for class:
- Failed to call addSuppressedMethod
AI-assisted analysis of java-native-access/jna@d036ad9781 (2026-09-12).
Data as JSON: /api/errors/9146f818b5df5d4c.
Report an issue: GitHub.