stanfordnlp/CoreNLP · error · ClassCreationException
MetaClass couldn't create " + constructor + " with args " +
Error message
MetaClass couldn't create " + constructor + " with args " + Arrays.toString(params)
What it means
MetaClass's reflective constructor-invocation helper wraps any exception raised by Constructor.newInstance (e.g. the target constructor threw, or was inaccessible/abstract) into a ClassCreationException. The message includes the constructor and the exact argument values used, so you can see what instantiation attempt failed. This is the Stanford CoreNLP reflection-based factory failing to build an object, typically from a properties-specified class.
Solutions
- Inspect the wrapped cause 'e' printed with this exception — it contains the real failure from the target constructor
- Verify the constructor arguments' types exactly match a declared constructor (reflection requires exact types, no autoboxing/coercion)
- Confirm the class is public, concrete (not abstract/interface), and has a matching constructor
- Test the class directly with 'new MyClass(args)' outside MetaClass to reproduce the underlying error
Example fix
// before
Properties props = new Properties();
props.setProperty("annotators", "myAnnotator"); // class whose ctor throws
// after: fix the target class constructor to not throw with given args, or use the no-arg ctor
props.setProperty("customAnnotator.myAnnotator.constructorArgs", ""); // plus fix ctor Defensive patterns
Strategy: try-catch
Validate before calling
// before createInstance
Class<?> cls = Class.forName("com.example.MyTypeImpl");
if (java.lang.reflect.Modifier.isAbstract(cls.getModifiers())) throw new IllegalStateException("abstract class");
for (Constructor<?> c : cls.getConstructors()) {
if (Arrays.equals(c.getParameterTypes(), new Class[]{String.class, int.class})) { /* exact match found */ }
} Try / catch
try {
T obj = metaClass.createInstance(Target.class, params);
} catch (MetaClass.ClassCreationException e) {
logger.log(Level.SEVERE, "Instantiation failed for args " + Arrays.toString(params), e.getCause());
throw new IllegalStateException("Bad class/args in config", e.getCause());
} Prevention
- Always log/print the cause chain — the real error is inside the wrapped exception
- Keep target classes public with public no-arg or exact-signature constructors
- Test any class referenced from properties files with a direct 'new' in unit tests
- Avoid renaming/refactoring classes that appear in persisted configuration
When it happens
Trigger: Calling MetaClass.createInstance(Class,Object...) or createInstance(Object...) where the resolved constructor throws an exception during execution, the constructor is not accessible and setAccessible fails, or the class is abstract/interface with no concrete constructor.
Common situations: Specifying a custom annotator/serializer class name in a CoreNLP properties file whose constructor throws; passing wrong constructor argument types so reflection resolves a constructor that then fails; refactoring a class after serializing a config that referenced it.
Related errors
- Loading: ${postProcessorClass} failed with: ${e.getMessage()
- Cannot cast " + classname + " into " + type.getName()
- Shouldn't happen:
- Error reading saved links
- RuntimeIOException wrapping IOException
AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10).
Data as JSON: /api/errors/d0191c0f609bb6e0.
Report an issue: GitHub.
Appendix: source
Thrown at src/edu/stanford/nlp/util/MetaClass.java:237
* @param params
* The arguments to the constructor of the class NOTE: the
* resulting instance will [unlike java] invoke the most
* narrow constructor rather than the one which matches the
* signature passed to this function
* @return An instance of the class
*/
public T createInstance(Object... params) {
try {
boolean accessible = true;
if(!constructor.isAccessible()){
accessible = false;
constructor.setAccessible(true);
}
T rtn = constructor.newInstance(params);
if(!accessible){ constructor.setAccessible(false); }
return rtn;
} catch (Exception e) {
throw new ClassCreationException("MetaClass couldn't create " + constructor + " with args " + Arrays.toString(params), e);
}
}
/**
* Returns the full class name for the objects being produced
*
* @return The class name for the objects produced
*/
public String getName() {
return cl.getName();
}
@Override
public String toString() {
StringBuilder b = new StringBuilder();
b.append(cl.getName()).append('(');
for (Class<?> cl : classParams) {
b.append(' ').append(cl.getName()).append(',');View on GitHub (pinned to 1b7edd19c4)