stanfordnlp/CoreNLP · error · RuntimeException
Error occurred while constructing TreeReaderFactory: " + e
Error message
Error occurred while constructing TreeReaderFactory: " + e
What it means
TregexPattern.getTreeReaderFactory loads a custom TreeReaderFactory class by reflection from the -treeReaderFactory command-line option. If the class name cannot be found, instantiated, or cast to TreeReaderFactory, the raw reflective exception is wrapped in a RuntimeException and rethrown.
Solutions
- Verify the fully-qualified class name passed to -treeReaderFactory is spelled correctly and the class implements edu.stanford.nlp.trees.TreeReaderFactory.
- Ensure the jar/class file is on the runtime classpath (check with `java -cp ...` and try loading the class via Class.forName manually).
- Confirm the class has a public no-argument constructor; add one if missing.
- Inspect the nested exception `e` in the message (and its stack trace) to see the actual reflective failure cause.
Example fix
// before java edu.stanford.nlp.trees.tregex.TregexPattern -treeReaderFactory MyFact treeFile // after (correct, on-classpath class implementing TreeReaderFactory) java -cp .:stanford-corenlp.jar edu.stanford.nlp.trees.tregex.TregexPattern -treeReaderFactory com.example.MyTreeReaderFactory treeFile
Defensive patterns
Strategy: validation
Validate before calling
try {
Class<?> c = Class.forName(factoryClassName);
if (!TreeReaderFactory.class.isAssignableFrom(c)) throw new IllegalArgumentException(factoryClassName + " is not a TreeReaderFactory");
c.getDeclaredConstructor().newInstance();
} catch (Exception e) {
throw new IllegalArgumentException("Bad -treeReaderFactory class: " + factoryClassName, e);
} Type guard
static boolean isValidTreeReaderFactory(String name) {
try {
return Class.forName(name) != null && TreeReaderFactory.class.isAssignableFrom(Class.forName(name));
} catch (Exception e) { return false; }
} Try / catch
try {
Treebank tb = ...; // runs with the custom factory
} catch (RuntimeException e) {
if (e.getMessage() != null && e.getMessage().contains("constructing TreeReaderFactory")) {
System.err.println("Fix -treeReaderFactory option: " + e.getCause());
} else throw e;
} Prevention
- Always pass the fully-qualified class name and keep the factory class on the runtime classpath.
- Keep a public no-arg constructor in custom TreeReaderFactory implementations.
- Smoke-test the factory class with a tiny treebank before long batch runs.
When it happens
Trigger: Running the TregexPattern main() with -treeReaderFactory <className> where className is misspelled, not on the classpath, lacks a no-arg constructor, its constructor throws, or it does not implement TreeReaderFactory (ClassCastException).
Common situations: Typo in the fully-qualified class name; jar containing the custom factory not on the runtime classpath; factory class was refactored/renamed across versions; constructor requires arguments or throws during initialization.
Related errors
- Class is in classpath multiple times
- Could not set option
- Error loading flags.readerAndWriter
- Error loading flags.plainTextDocumentReaderAndWriter
- Unknown class
AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10).
Data as JSON: /api/errors/9ebf6abc3a9ac765.
Report an issue: GitHub.
Appendix: source
Thrown at src/edu/stanford/nlp/trees/tregex/TregexPattern.java:828
}
if (TRegexTreeVisitor.printNumMatchesToStdOut) {
System.out.println(vis.numMatches());
}
} catch (IOException e) {
log.warn(e);
} catch (TregexParseException e) {
errPW.println("Error parsing expression: " + args[0]);
errPW.println("Parse exception: " + e);
}
}
private static TreeReaderFactory getTreeReaderFactory(String treeReaderFactoryClassName) {
TreeReaderFactory trf = new TRegexTreeReaderFactory();
if (treeReaderFactoryClassName != null) {
try {
trf = (TreeReaderFactory) Class.forName(treeReaderFactoryClassName).getDeclaredConstructor().newInstance();
} catch (Exception e) {
throw new RuntimeException("Error occurred while constructing TreeReaderFactory: " + e);
}
}
return trf;
}
private static Treebank treebank; // used by main method, must be accessible
private static final long serialVersionUID = 5060298043763944913L;
// not thread-safe, but only used by TregexPattern's main method
private static class TRegexTreeVisitor implements TreeVisitor {
private static boolean printNumMatchesToStdOut = false;
static boolean printNonMatchingTrees = false;
static boolean printSubtreeCode = false;
static boolean printTree = false;
static boolean printWholeTreeOnly = false;View on GitHub (pinned to 1b7edd19c4)