{"record":{"id":"8b5aff8eff44c20b","repo":"stanfordnlp/CoreNLP","slug":"invalid-class-in-file","errorCode":null,"errorMessage":"Invalid class in file: ","messagePattern":"Invalid class in file: ","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/parser/lexparser/LexicalizedParser.java","lineNumber":577,"sourceCode":"    } catch (IOException e) {\n      e.printStackTrace();\n    }\n    return null;\n  }\n\n\n  public static LexicalizedParser getParserFromSerializedFile(String serializedFileOrUrl) {\n    try {\n      Timing tim = new Timing();\n      ObjectInputStream in = IOUtils.readStreamFromString(serializedFileOrUrl);\n      LexicalizedParser pd = loadModel(in);\n\n      in.close();\n      log.info(\"Loading parser from serialized file \" + serializedFileOrUrl + \" ... done [\" + tim.toSecondsString() + \" sec].\");\n      return pd;\n    } catch (InvalidClassException ice) {\n      // For this, it's not a good idea to continue and try it as a text file!\n      throw new RuntimeException(\"Invalid class in file: \" + serializedFileOrUrl, ice);\n    } catch (FileNotFoundException fnfe) {\n      // For this, it's not a good idea to continue and try it as a text file!\n      throw new RuntimeException(\"File not found: \" + serializedFileOrUrl, fnfe);\n    } catch (StreamCorruptedException sce) {\n      // suppress error message, on the assumption that we've really got\n      // a text grammar, and that'll be tried next\n      log.info(\"Attempting to load \" + serializedFileOrUrl +\n               \" as a serialized grammar caused error below, but this may just be because it's a text grammar!\");\n      log.info(sce);\n    } catch (Exception e) {\n      log.error(e);\n    }\n    return null;\n  }\n\n\n  private static void printOptions(boolean train, Options op) {\n    op.display();","sourceCodeStart":559,"sourceCodeEnd":595,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/parser/lexparser/LexicalizedParser.java#L559-L595","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\nLexicalizedParser lp = LexicalizedParser.loadModel(\"model-3.6.0.ser.gz\"); // with CoreNLP 4.x jar\n// after\nLexicalizedParser lp = LexicalizedParser.loadModel(\"englishPCFG-4.5.0.ser.gz\"); // version-matched model","handlingStrategy":"try-catch","validationCode":"// Check jar version vs. expected model version before loading\nString version = LexicalizedParser.class.getPackage().getImplementationVersion();\n// ensure model file was built for this version (versioned filenames help)","typeGuard":null,"tryCatchPattern":"try {\n  LexicalizedParser lp = LexicalizedParser.loadModel(path);\n} catch (RuntimeException e) {\n  if (e.getCause() instanceof InvalidClassException) {\n    throw new IllegalStateException(\"Model/library version mismatch for \" + path + \": \" + e.getCause().getMessage(), e);\n  }\n  throw e;\n}","preventionTips":["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"],"tags":["serialization","version-mismatch","classpath","java"],"backgroundTag":"class-not-found","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}