stanfordnlp/CoreNLP · error · RuntimeException

Couldn't instantiate CategoryMergingGrammarCompactor.

Error message

Couldn't instantiate CategoryMergingGrammarCompactor.

What it means

GrammarCompactionTester.runTest tries to reflectively instantiate fsm.CategoryMergingGrammarCompactor when compactGrammar() == 2. Because that class lives in an FSM package that is not always shipped/compiled, Class.forName or the constructor fails, and any Exception is rethrown wrapped in this RuntimeException (message includes the underlying cause).

Solutions

  1. Ensure the fsm jar/classes (CategoryMergingGrammarCompactor) are on the classpath.
  2. Use compactGrammar == 3 (ExactGrammarCompactor) instead, which is bundled in the lexparser codebase.
  3. Check that the six compactor arguments (argTypes/cArgs) still match the constructor for your library version.

Example fix

// before
op.trainOptions.compactGrammar = 2; // requires external fsm classes
// after
op.trainOptions.compactGrammar = 3; // uses bundled ExactGrammarCompactor
Defensive patterns

Strategy: try-catch

Validate before calling

try { Class.forName("fsm.CategoryMergingGrammarCompactor"); }
catch (ClassNotFoundException e) { /* fall back to ExactGrammarCompactor (compactGrammar=3) */ }

Try / catch

try { runTest(); } catch (RuntimeException e) { if (e.getMessage().startsWith("Couldn't instantiate")) { op.trainOptions.compactGrammar = 3; } }

Prevention

When it happens

Trigger: Setting trainOptions.compactGrammar to 2 while fsm.CategoryMergingGrammarCompactor is not on the classpath or its constructor signature/arguments don't match.

Common situations: Running grammar compaction tests with a partial build that excludes the fsm classes, or after a version change altered the compactor's constructor arguments.

Related errors


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

Appendix: source

Thrown at src/edu/stanford/nlp/parser/lexparser/GrammarCompactionTester.java:249

      }
    } else if (op.trainOptions.compactGrammar() == 5) {
      System.out.println("Instantiating fsm.CategoryMergingGrammarCompactor");
      try {
        Class[] argTypes = new Class[6];
        Class strClass = String.class;
        for (int j = 0; j < argTypes.length; j++) {
          argTypes[j] = strClass;
        }
        Object[] cArgs = new Object[6];
        cArgs[0] = splitParamString;
        cArgs[1] = trainThresholdString;
        cArgs[2] = heldoutThresholdString;
        cArgs[3] = minArcCostString;
        cArgs[4] = ignoreUnsupportedSuffixesString;
        cArgs[5] = smoothParamString;
        compactor = (GrammarCompactor) Class.forName("fsm.CategoryMergingGrammarCompactor").getConstructor(argTypes).newInstance(cArgs);
      } catch (Exception e) {
        throw new RuntimeException("Couldn't instantiate CategoryMergingGrammarCompactor." + e);
      }
    } else if (op.trainOptions.compactGrammar() == 3) {
      System.out.println("Instantiating fsm.ExactGrammarCompactor");
      compactor = new ExactGrammarCompactor(op, saveGraphs, true);
    } else if (op.trainOptions.compactGrammar() > 0) {
    }

    if (markovOrder >= 0) {
      op.trainOptions.markovOrder = markovOrder;
      op.trainOptions.hSelSplit = false;
    }
    if (toy) {
      buildAndCompactToyGrammars();
    } else {
      testGrammarCompaction();
    }
  }

View on GitHub (pinned to 1b7edd19c4)