stanfordnlp/CoreNLP · error · RuntimeException
Invalid class in file:
Error message
Invalid class in file:
What it means
When loading a parser from a serialized file, the object stream read failed with java.io.InvalidClassException. LexicalizedParser.getParserFromSerializedFile deliberately wraps this in a RuntimeException ("Invalid class in file: ...") because retrying the file as a text grammar would be pointless. It indicates the serialized class descriptors in the file do not match the classes on the classpath.
Solutions
- Align jar and model versions: use a parser model file built with (or known to be compatible with) the exact library version on your classpath
- Re-serialize/retrain the parser with the current library version, or re-download the matching pre-trained model from the Stanford model release page
- Check for duplicate/stale parser jars (mvn dependency:tree, look in WEB-INF/lib, or jarseverywhere issues) and remove the conflicting one
- Print the nested InvalidClassException's serialVersionUID detail to see which class mismatched and restore that class version
Example fix
// before
LexicalizedParser lp = LexicalizedParser.loadModel("model-3.6.0.ser.gz"); // with CoreNLP 4.x jar
// after
LexicalizedParser lp = LexicalizedParser.loadModel("englishPCFG-4.5.0.ser.gz"); // version-matched model Defensive patterns
Strategy: try-catch
Validate before calling
// Check jar version vs. expected model version before loading String version = LexicalizedParser.class.getPackage().getImplementationVersion(); // ensure model file was built for this version (versioned filenames help)
Try / catch
try {
LexicalizedParser lp = LexicalizedParser.loadModel(path);
} catch (RuntimeException e) {
if (e.getCause() instanceof InvalidClassException) {
throw new IllegalStateException("Model/library version mismatch for " + path + ": " + e.getCause().getMessage(), e);
}
throw e;
} Prevention
- Pin model files and parser jars to the same release version
- Audit classpath for duplicate or stale stanford parser jars
- Retrain/re-serialize custom models after any library upgrade
When it happens
Trigger: Calling LexicalizedParser.loadModel/getParserFromSerializedFile on a .ser/.ser.gz model serialized by a different (usually older or newer) version of the parser classes than the ones on the classpath, or on a corrupt file.
Common situations: Upgrading the Stanford Parser/CoreNLP jar while keeping an old trained model; mixing parser jars on the classpath so a stale class shadows the right one; a model trained with a custom TreebankLanguagePack/Options class that was later renamed or changed serial version.
Related errors
- Invalid model at path: " + serializedModel
- Unknown general relation <shortName>
- is not a legal LogPrior.
- check flag in markMentionRelation (dcoref/MentionExtractor.j
- RuntimeIOException wrapping IOException
AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10).
Data as JSON: /api/errors/8b5aff8eff44c20b.
Report an issue: GitHub.
Appendix: source
Thrown at src/edu/stanford/nlp/parser/lexparser/LexicalizedParser.java:577
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static LexicalizedParser getParserFromSerializedFile(String serializedFileOrUrl) {
try {
Timing tim = new Timing();
ObjectInputStream in = IOUtils.readStreamFromString(serializedFileOrUrl);
LexicalizedParser pd = loadModel(in);
in.close();
log.info("Loading parser from serialized file " + serializedFileOrUrl + " ... done [" + tim.toSecondsString() + " sec].");
return pd;
} catch (InvalidClassException ice) {
// For this, it's not a good idea to continue and try it as a text file!
throw new RuntimeException("Invalid class in file: " + serializedFileOrUrl, ice);
} catch (FileNotFoundException fnfe) {
// For this, it's not a good idea to continue and try it as a text file!
throw new RuntimeException("File not found: " + serializedFileOrUrl, fnfe);
} catch (StreamCorruptedException sce) {
// suppress error message, on the assumption that we've really got
// a text grammar, and that'll be tried next
log.info("Attempting to load " + serializedFileOrUrl +
" as a serialized grammar caused error below, but this may just be because it's a text grammar!");
log.info(sce);
} catch (Exception e) {
log.error(e);
}
return null;
}
private static void printOptions(boolean train, Options op) {
op.display();View on GitHub (pinned to 1b7edd19c4)