{"record":{"id":"10a133f1d7e977da","repo":"stanfordnlp/CoreNLP","slug":"error-occurred-while-constructing-headfinder","errorCode":null,"errorMessage":"Error occurred while constructing HeadFinder: \" + e","messagePattern":"Error occurred while constructing HeadFinder: \" \\+ e","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/trees/tregex/TregexPattern.java","lineNumber":768,"sourceCode":"    } else if (argsMap.containsKey(yieldOnly)) {\n      treePrintFormats.append(\"words,\");\n    } else {\n      treePrintFormats.append(\"penn,\");\n    }\n    if (argsMap.containsKey(uniqueTrees)) {\n      TRegexTreeVisitor.printOnlyUniqueTrees = true;\n    }\n\n    HeadFinder hf = new CollinsHeadFinder();\n    if(headFinderClassName != null) {\n      Class[] hfArgClasses = new Class[headFinderArgs.length];\n      for (int i = 0; i < hfArgClasses.length; i++) {\n        hfArgClasses[i] = String.class;\n      }\n      try {\n        hf = (HeadFinder) Class.forName(headFinderClassName).getConstructor(hfArgClasses).newInstance((Object[]) headFinderArgs); // cast to Object[] necessary to avoid varargs-related warning.\n      }\n      catch(Exception e) { throw new RuntimeException(\"Error occurred while constructing HeadFinder: \" + e); }\n    }\n\n    TRegexTreeVisitor.tp = new TreePrint(treePrintFormats.toString(), new PennTreebankLanguagePack());\n\n    try {\n      //TreePattern p = TreePattern.compile(\"/^S/ > S=dt $++ '' $-- ``\");\n      TregexPatternCompiler tpc = new TregexPatternCompiler(hf);\n      Macros.addAllMacros(tpc, macroFilename, encoding);\n      TregexPattern p = tpc.compile(matchString);\n      errPW.println(\"Pattern string:\\n\" + p.pattern());\n      errPW.println(\"Parsed representation:\");\n      p.prettyPrint(errPW);\n\n      String[] handles = argsMap.get(printHandleOption);\n      if (argsMap.containsKey(\"-filter\")) {\n        TreeReaderFactory trf = getTreeReaderFactory(treeReaderFactoryClassName);\n        treebank = new MemoryTreebank(trf, encoding);//has to be in memory since we're not storing it on disk\n        //read from stdin","sourceCodeStart":750,"sourceCodeEnd":786,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/trees/tregex/TregexPattern.java#L750-L786","documentation":"TregexPattern's command-line main allows specifying a custom HeadFinder class (with optional constructor arguments). The class is loaded reflectively via Class.forName and instantiated via getConstructor(...).newInstance(...); any failure (class missing, no matching constructor, instantiation error) is rethrown as RuntimeException('Error occurred while constructing HeadFinder: ' + e).","triggerScenarios":"Running the TregexPattern main with -headFinder specifying a class that is not on the classpath, lacks a constructor matching the supplied String args, throws in its constructor, or is otherwise not instantiable as a HeadFinder.","commonSituations":"Typo in the fully-qualified class name; headfinder jar not on the classpath; changing a headfinder's constructor signature without updating CLI args; using a class that does not implement HeadFinder.","solutions":["Verify the fully-qualified class name and that its jar is on the classpath","Check the HeadFinder class has a public constructor matching the supplied arguments (Strings)","Instantiate the HeadFinder in code first to see the underlying stack trace","Pass a HeadFinder programmatically instead of via the CLI flag"],"exampleFix":"// before\njava ... TregexPattern -headFinder com.example.MyHF,extraArg pattern file\n// after\njava -cp .:myhf.jar ... TregexPattern -headFinder com.example.MyHF pattern file\n// where MyHF has a public MyHF(String...) constructor","handlingStrategy":"try-catch","validationCode":"try {\n  Class<?> c = Class.forName(headFinderClassName);\n  if (!HeadFinder.class.isAssignableFrom(c)) throw new IllegalArgumentException(c + \" is not a HeadFinder\");\n  c.getConstructor(String[].class);\n} catch (ClassNotFoundException | NoSuchMethodException e) { /* fix name/classpath/constructor */ }","typeGuard":"boolean isUsableHeadFinder(String name) {\n  try { return HeadFinder.class.isAssignableFrom(Class.forName(name)); }\n  catch (ClassNotFoundException e) { return false; }\n}","tryCatchPattern":"try { TregexPattern.main(args); } catch (RuntimeException e) { if (e.getMessage().startsWith(\"Error occurred while constructing HeadFinder\")) { e.printStackTrace(); /* check class name, classpath, constructor args */ } }","preventionTips":["Verify the fully-qualified HeadFinder class name and classpath before running","Ensure the class implements HeadFinder and has a constructor matching the supplied args","Instantiate the HeadFinder in a small test program to surface the root cause","Prefer programmatic HeadFinder injection over CLI flags where possible"],"tags":["tregex","reflection","class-not-found","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-17T15:17:12.973Z"}