stanfordnlp/CoreNLP · error · ClassCreationException

Class " + classname + " could not be cast to the correct…

Error message

Class " + classname + " could not be cast to the correct type

What it means

MetaClass.ClassFactory loads a class by name via Class.forName and casts the resulting Class<?> to Class<T>. If the loaded class cannot be cast to the expected type parameter (e.g. the requested classname is not assignable to the generic target type), a ClassCreationException wraps the failure with the offending classname in the message.

Solutions

  1. Verify the configured classname implements/extends the expected component interface
  2. Print Class.forName(classname).getInterfaces()/superclass to inspect the loaded type
  3. Fix the classname in the configuration file or code that passes it
  4. Rebuild against the correct library version if the class hierarchy changed

Example fix

// before
props.setProperty("classifier", "edu.stanford.nlp.tagger.MaxentTagger"); // not a Classifier
// after
props.setProperty("classifier", "edu.stanford.nlp.classify.ColumnDataClassifier");
Defensive patterns

Strategy: try-catch

Validate before calling

Class<?> c;
try { c = Class.forName(classname); }
catch (ClassNotFoundException e) { throw new IllegalArgumentException("unknown class: " + classname); }
if (!ExpectedType.class.isAssignableFrom(c)) {
  throw new IllegalArgumentException(classname + " does not implement " + ExpectedType.class.getName());
}

Type guard

boolean isAssignableTo(String classname, Class<?> target) {
  try { return target.isAssignableFrom(Class.forName(classname)); }
  catch (ClassNotFoundException e) { return false; }
}

Try / catch

try {
  T obj = MetaClass.create(classname).createInstance(args);
} catch (MetaClass.ClassCreationException e) {
  // wrong type: log classname and expected type, fix config
}

Prevention

When it happens

Trigger: Configuring a classname that does not extend/implement the expected type, e.g. pointing a pipeline component option at a class of the wrong interface; the ClassCastException occurs at the unchecked cast inside the ClassFactory constructor.

Common situations: Typo'd or wrong FQCN in properties/serialized options, upgrading a library where a class moved or changed its interface, copy-pasting a class name from a different component type.

Related errors


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

Appendix: source

Thrown at src/edu/stanford/nlp/util/MetaClass.java:137

          }
        }
        if (minDist != Integer.MAX_VALUE)
          return minDist + 1; // case: interface distance
        else
          return -1; // case: failure
      }
    }

    @SuppressWarnings("unchecked")
    private void construct(String classname, Class<?>... params)
        throws ClassNotFoundException, NoSuchMethodException {
      // (save class parameters)
      this.classParams = params;
      // (create class)
      try {
        this.cl = (Class<T>) Class.forName(classname);
      } catch (ClassCastException e) {
        throw new ClassCreationException("Class " + classname
            + " could not be cast to the correct type");
      }
      // --Find Constructor
      // (get constructors)
      Constructor<?>[] constructors = cl.getDeclaredConstructors();
      Constructor<?>[] potentials = new Constructor<?>[constructors.length];
      Class<?>[][] constructorParams = new Class<?>[constructors.length][];
      int[] distances = new int[constructors.length]; //distance from base class
      // (filter: length)
      for (int i = 0; i < constructors.length; i++) {
        constructorParams[i] = constructors[i].getParameterTypes();
        if (params.length == constructorParams[i].length) { // length is good
          potentials[i] = constructors[i];
          distances[i] = 0;
        } else { // length is bad
          potentials[i] = null;
          distances[i] = -1;
        }

View on GitHub (pinned to 1b7edd19c4)