stanfordnlp/CoreNLP · error · RuntimeException

Bad serialized file:

Error message

Bad serialized file: 

What it means

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.

Solutions

  1. Verify the -lex path points to the actual serialized character lexicon (e.g. parsers/chineseCharLex.ser.gz), not a different model file
  2. Add the Stanford Parser jars matching the model version to the classpath so all serialized classes resolve
  3. Re-download or regenerate the serialized lexicon file
  4. Temporarily log the ClassNotFoundException cause to see exactly which class is missing

Example fix

// before
} catch (ClassNotFoundException e) {
  throw new RuntimeException("Bad serialized file: " + lexFile);
}
// after
} catch (ClassNotFoundException e) {
  throw (RuntimeException) new RuntimeException("Bad serialized file: " + lexFile).initCause(e);
}
Defensive patterns

Strategy: validation

Validate before calling

// before passing to -lex, sanity check the file
File f = new File(lexPath);
if (!f.isFile() || f.length() == 0)
  throw new IllegalArgumentException("Lexicon file missing or empty: " + lexPath);

Try / catch

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()

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10). Data as JSON: /api/errors/8c73c53a01121e7f. Report an issue: GitHub.

Appendix: source

Thrown at src/edu/stanford/nlp/parser/lexparser/ChineseCharacterBasedLexiconTraining.java:264

        lex.finishTraining();
        log.info("Done training lexicon.");
        if (lexArgs.length == 3) {
          String filename = lexArgs.length == 3 ? lexArgs[2] : "parsers/chineseCharLex.ser.gz";
          log.info("Writing lexicon in serialized format to file " + filename + " ");
          System.err.flush();
          ObjectOutputStream out = IOUtils.writeStreamFromString(filename);
          out.writeObject(lex);
          out.close();
          log.info("done.");
        }
      } else {
        String lexFile = lexArgs.length == 1 ? lexArgs[0] : "parsers/chineseCharLex.ser.gz";
        log.info("Reading Lexicon from file " + lexFile);
        ObjectInputStream in = IOUtils.readStreamFromString(lexFile);
        try {
          lex = (Lexicon) in.readObject();
        } catch (ClassNotFoundException e) {
          throw new RuntimeException("Bad serialized file: " + lexFile);
        }
        in.close();
      }
    }

    if (argMap.containsKey("-test")) {
      boolean segmentWords = ctpp.segment;
      boolean parse = lp != null;
      assert (parse || segmentWords);
      //      WordCatConstituent.collinizeWords = argMap.containsKey("-collinizeWords");
      //      WordCatConstituent.collinizeTags = argMap.containsKey("-collinizeTags");
      WordSegmenter seg = null;
      if (segmentWords) {
        seg = (WordSegmenter) lex;
      }
      String[] testArgs = (argMap.get("-test"));
      MemoryTreebank testTreebank = op.tlpParams.memoryTreebank();
      FileFilter testFilt = new NumberRangesFileFilter(testArgs[1], false);

View on GitHub (pinned to 1b7edd19c4)