stanfordnlp/CoreNLP · error · RuntimeIOException

Couldn't read TokensRegexNER from

Error message

Couldn't read TokensRegexNER from ${mapping}

What it means

The constructor reads each configured mapping file with IOUtils.readerFromString and wraps any IOException as RuntimeIOException('Couldn't read TokensRegexNER from <mapping>'). It signals the mapping resource could not be opened, whether because it does not exist, is unreachable, or the URL/classpath location is wrong.

Solutions

  1. Verify the path in the 'mapping' property exists and is readable from the process's working directory.
  2. Use a classpath prefix for bundled resources, e.g. 'mapping=classpath:/custom_mappings.txt' or pass the resource name shipped in your JAR.
  3. Use an absolute path or file: URL to eliminate working-directory ambiguity.
  4. Catch RuntimeIOException at construction time and fall back to a default mapping.

Example fix

// before
props.setProperty("tokensregexner.mapping", "my_rules.txt");
// after
props.setProperty("tokensregexner.mapping", "/etc/corenlp/my_rules.txt");
Defensive patterns

Strategy: try-catch

Validate before calling

String mapping = props.getProperty("tokensregexner.mapping");
java.io.File f = new java.io.File(mapping);
if (!f.canRead() && !mapping.startsWith("classpath") && !mapping.matches("^https?://.*")) {
  throw new IllegalStateException("Mapping not readable: " + f.getAbsolutePath());
}

Try / catch

try { annotator = new TokensRegexNERAnnotator(name, props); } catch (RuntimeIOException e) { logger.warn("Falling back to default mapping", e); props.setProperty("tokensregexner.mapping", DefaultPaths.DEFAULT_TOKENSREGEXNER_MAPPINGS); annotator = new TokensRegexNERAnnotator(name, props); }

Prevention

When it happens

Trigger: Setting tokensregexner.mapping to a file path that doesn't exist, a classpath resource name not on the classpath, or a malformed/unreachable URL; also IO errors mid-read (permissions, truncated network file).

Common situations: Typo in mapping filename; running from a different working directory with a relative path; deploying a JAR without bundling the custom mapping resource; Windows vs Unix path separators.

Understand the failure class

Background: "failed to read file", EACCES, ENOENT and "could not read <path>" errors: when a program can't read a file from disk — this error's family across 49 libraries.

Related errors


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

Appendix: source

Thrown at src/edu/stanford/nlp/pipeline/TokensRegexNERAnnotator.java:622

                                         List<Boolean> ignoreCaseList,
                                         List<String[]> headerList,
                                         Map<Entry,Integer> entryToMappingFileNumber,
                                         boolean verbose,
                                         String[] annotationFieldnames,
                                         String... mappings) {
    // Unlike RegexNERClassifier, we don't bother sorting the entries.
    // We leave it to TokensRegex NER to sort out the priorities and matches
    // (typically after all the matches has been made since for some TokensRegex expressions,
    // we don't know how many tokens are matched until after the matching is done).
    List<Entry> entries = new ArrayList<>();
    TrieMap<String,Entry> seenRegexes = new TrieMap<>();
    // Arrays.sort(mappings);
    for (int mappingFileIndex = 0; mappingFileIndex < mappings.length; mappingFileIndex++) {
      String mapping = mappings[mappingFileIndex];
      try (BufferedReader rd = IOUtils.readerFromString(mapping)){
        readEntries(annotatorName, headerList.get(mappingFileIndex), annotationFieldnames, entries, seenRegexes, mapping, rd, noDefaultOverwriteLabels, ignoreCaseList.get(mappingFileIndex), mappingFileIndex, entryToMappingFileNumber, verbose);
      } catch (IOException e) {
        throw new RuntimeIOException("Couldn't read TokensRegexNER from " + mapping, e);
      }
    }

    if (mappings.length != 1) {
      logger.log(annotatorName + ": Read " + entries.size() + " unique entries from " + mappings.length + " files");
    }
    return entries;
  }

  private static Map<String,Integer> getHeaderIndexMap(String[] headerFields) {
    Map<String,Integer> map = new HashMap<>();
    for (int i = 0; i < headerFields.length; i++) {
      String field = headerFields[i];
      if (map.containsKey(field)) {
        throw new IllegalArgumentException("Duplicate header field: " + field);
      }
      map.put(field,i);
    }

View on GitHub (pinned to 1b7edd19c4)