{"record":{"id":"8c73c53a01121e7f","repo":"stanfordnlp/CoreNLP","slug":"bad-serialized-file","errorCode":null,"errorMessage":"Bad serialized file: ","messagePattern":"Bad serialized file: ","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/parser/lexparser/ChineseCharacterBasedLexiconTraining.java","lineNumber":264,"sourceCode":"        lex.finishTraining();\n        log.info(\"Done training lexicon.\");\n        if (lexArgs.length == 3) {\n          String filename = lexArgs.length == 3 ? lexArgs[2] : \"parsers/chineseCharLex.ser.gz\";\n          log.info(\"Writing lexicon in serialized format to file \" + filename + \" \");\n          System.err.flush();\n          ObjectOutputStream out = IOUtils.writeStreamFromString(filename);\n          out.writeObject(lex);\n          out.close();\n          log.info(\"done.\");\n        }\n      } else {\n        String lexFile = lexArgs.length == 1 ? lexArgs[0] : \"parsers/chineseCharLex.ser.gz\";\n        log.info(\"Reading Lexicon from file \" + lexFile);\n        ObjectInputStream in = IOUtils.readStreamFromString(lexFile);\n        try {\n          lex = (Lexicon) in.readObject();\n        } catch (ClassNotFoundException e) {\n          throw new RuntimeException(\"Bad serialized file: \" + lexFile);\n        }\n        in.close();\n      }\n    }\n\n    if (argMap.containsKey(\"-test\")) {\n      boolean segmentWords = ctpp.segment;\n      boolean parse = lp != null;\n      assert (parse || segmentWords);\n      //      WordCatConstituent.collinizeWords = argMap.containsKey(\"-collinizeWords\");\n      //      WordCatConstituent.collinizeTags = argMap.containsKey(\"-collinizeTags\");\n      WordSegmenter seg = null;\n      if (segmentWords) {\n        seg = (WordSegmenter) lex;\n      }\n      String[] testArgs = (argMap.get(\"-test\"));\n      MemoryTreebank testTreebank = op.tlpParams.memoryTreebank();\n      FileFilter testFilt = new NumberRangesFileFilter(testArgs[1], false);","sourceCodeStart":246,"sourceCodeEnd":282,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/parser/lexparser/ChineseCharacterBasedLexiconTraining.java#L246-L282","documentation":"In ChineseCharacterBasedLexiconTraining.main, after reading the serialized lexicon file, a ClassNotFoundException during readObject is converted to RuntimeException(\"Bad serialized file: \" + lexFile). It means the stream is not a valid lexicon object graph for this classpath.","triggerScenarios":"Running the -lex main with a lexicon file that is not a serialized Lexicon (wrong file, text file, or gzipped data of the wrong type) or that references classes missing from the classpath.","commonSituations":"Pointing the -lex argument at a parser grammar instead of a character lexicon, mismatched jar versions on the classpath, corrupted download of the serialized model.","solutions":["Verify the -lex path points to the actual serialized character lexicon (e.g. parsers/chineseCharLex.ser.gz), not a different model file","Add the Stanford Parser jars matching the model version to the classpath so all serialized classes resolve","Re-download or regenerate the serialized lexicon file","Temporarily log the ClassNotFoundException cause to see exactly which class is missing"],"exampleFix":"// before\n} catch (ClassNotFoundException e) {\n  throw new RuntimeException(\"Bad serialized file: \" + lexFile);\n}\n// after\n} catch (ClassNotFoundException e) {\n  throw (RuntimeException) new RuntimeException(\"Bad serialized file: \" + lexFile).initCause(e);\n}","handlingStrategy":"validation","validationCode":"// before passing to -lex, sanity check the file\nFile f = new File(lexPath);\nif (!f.isFile() || f.length() == 0)\n  throw new IllegalArgumentException(\"Lexicon file missing or empty: \" + lexPath);","typeGuard":null,"tryCatchPattern":"try { lex = (Lexicon) in.readObject(); } catch (ClassNotFoundException e) { throw new RuntimeException(\"Bad serialized file: \" + lexFile + \" (missing class \" + e.getMessage() + \")\").initCause(e) == null ? null : null; } // prefer: wrap with initCause(e) and log e.getMessage()","preventionTips":["Confirm the -lex argument points to the character lexicon, not another model type","Keep classpath jars version-matched to the serialized model","Re-download corrupted model files and verify file size/checksum"],"tags":["serialization","classpath","lexicon","cli"],"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"}