{"record":{"id":"a188eb5a674ce207","repo":"stanfordnlp/CoreNLP","slug":"error-loading-classifier-from","errorCode":null,"errorMessage":"Error loading classifier from ","messagePattern":"Error loading classifier from ","errorType":"exception","errorClass":"RuntimeIOException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/classify/MultinomialLogisticClassifier.java","lineNumber":126,"sourceCode":"  }\n\n  @Override\n  public Counter<L> logProbabilityOf(Datum<L, F> example) {\n    Counter<L> result = probabilityOf(example);\n    Counters.logInPlace(result);\n    return result;\n  }\n\n  private static <LL,FF> MultinomialLogisticClassifier<LL,FF> load(String path) {\n    Timing t = new Timing();\n    try (ObjectInputStream in = IOUtils.readStreamFromString(path)) {\n      double[][] myWeights = ErasureUtils.uncheckedCast(in.readObject());\n      Index<FF> myFeatureIndex = ErasureUtils.uncheckedCast(in.readObject());\n      Index<LL> myLabelIndex = ErasureUtils.uncheckedCast(in.readObject());\n      t.done(logger, \"Loading classifier from \" + path);\n      return new MultinomialLogisticClassifier<>(myWeights, myFeatureIndex, myLabelIndex);\n    } catch (IOException | ClassNotFoundException e) {\n      throw new RuntimeIOException(\"Error loading classifier from \" + path, e);\n    }\n  }\n\n  private void save(String path) throws IOException {\n    System.out.print(\"Saving classifier to \" + path + \"... \");\n\n    // make sure the directory specified by path exists\n    int lastSlash = path.lastIndexOf(File.separator);\n    if (lastSlash > 0) {\n      File dir = new File(path.substring(0, lastSlash));\n      if (! dir.exists())\n        dir.mkdirs();\n    }\n\n    ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(path));\n    out.writeObject(weights);\n    out.writeObject(featureIndex);\n    out.writeObject(labelIndex);","sourceCodeStart":108,"sourceCodeEnd":144,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/classify/MultinomialLogisticClassifier.java#L108-L144","documentation":"MultinomialLogisticClassifier.loadSelfSupervised/load deserializes a classifier (weights array, feature index, label index) from a file via ObjectInputStream. If the stream is corrupt, truncated, or not a saved classifier, an IOException or ClassNotFoundException is wrapped in this RuntimeIOException carrying the path. It is a deserialization failure, not a modeling error.","triggerScenarios":"Calling MultinomialLogisticClassifier.load(path) on a missing, corrupt, or wrong-format file; loading a classifier serialized by an incompatible library version (class shape changed → ClassNotFoundException); reading a text file or classifier of a different type.","commonSituations":"Pointing to the wrong path or a file moved/truncated; version mismatch after upgrading CoreNLP so the serialized class descriptor differs; attempting to load a LinearClassifier's dump as a MultinomialLogisticClassifier.","solutions":["Verify the path points to a file previously written by MultinomialLogisticClassifier.save with the same library version","Catch RuntimeIOException and check the cause (IOException vs ClassNotFoundException) to distinguish corrupt file from version mismatch","Re-save the classifier with the current library version and retry","Check file existence/readability before load"],"exampleFix":"// before\nMultinomialLogisticClassifier c = MultinomialLogisticClassifier.load(cfgPath);\n// after\nFile f = new File(modelPath);\nif (!f.isFile()) throw new IllegalArgumentException(\"model missing: \" + modelPath);\ntry {\n  MultinomialLogisticClassifier c = MultinomialLogisticClassifier.load(modelPath);\n} catch (RuntimeIOException e) {\n  throw new IllegalStateException(\"Incompatible/corrupt model at \" + modelPath, e);\n}","handlingStrategy":"try-catch","validationCode":"File f = new File(path);\nif (!f.exists() || !f.canRead() || f.length() < 8)\n  throw new IllegalArgumentException(\"Classifier file missing or empty: \" + path);","typeGuard":null,"tryCatchPattern":"try {\n  MultinomialLogisticClassifier<LL,FF> c = MultinomialLogisticClassifier.load(path);\n} catch (RuntimeIOException e) {\n  if (e.getCause() instanceof ClassNotFoundException)\n    throw new IllegalStateException(\"Model serialized with a different library version: \" + path, e);\n  throw new IllegalStateException(\"Corrupt or missing model file: \" + path, e);\n}","preventionTips":["Save and load models with the exact same library version; record the version alongside the model","Validate file existence and non-zero size before load","Catch RuntimeIOException (not IOException) since load wraps it","Never hand-edit or truncate serialized classifier files"],"tags":["java","io","deserialization","runtimeioexception","model-loading"],"backgroundTag":"file-read-failed","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"}