apache/hadoop · error · IllegalArgumentException

Failed to create uri for {}

Error message

Failed to create uri for {}

What it means

StringUtils.stringToURI(String[]) parses each configured string into a java.net.URI (used for classpath and cache-style config lists). Any entry violating RFC 2396 — spaces, backslashes, malformed brackets — makes it throw IllegalArgumentException wrapping the URISyntaxException, naming the offending string.

Source

Thrown at hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/StringUtils.java:261

  }
  
  /**
   * @param str
   *          The string array to be parsed into an URI array.
   * @return <code>null</code> if str is <code>null</code>, else the URI array
   *         equivalent to str.
   * @throws IllegalArgumentException
   *           If any string in str violates RFC&nbsp;2396.
   */
  public static URI[] stringToURI(String[] str){
    if (str == null) 
      return null;
    URI[] uris = new URI[str.length];
    for (int i = 0; i < str.length;i++){
      try{
        uris[i] = new URI(str[i]);
      }catch(URISyntaxException ur){
        throw new IllegalArgumentException(
            "Failed to create uri for " + str[i], ur);
      }
    }
    return uris;
  }
  
  /**
   * stringToPath.
   * @param str str.
   * @return path array.
   */
  public static Path[] stringToPath(String[] str){
    if (str == null) {
      return null;
    }
    Path[] p = new Path[str.length];
    for (int i = 0; i < str.length;i++){
      p[i] = new Path(str[i]);

View on GitHub (pinned to 2add963021)

Solutions

  1. Build URIs properly on the producer side: new File(path).toURI().toString() percent-encodes spaces and fixes separators
  2. Remove or quote spaces in configured paths, or percent-encode components (my%20files)
  3. Convert Windows backslash paths to forward-slash URI form before passing them in
  4. Pre-validate each entry with new URI(s) in a unit test to surface the exact index and position of the bad string

Example fix

// before
String[] paths = {"file:/data/my files/tool.jar"}; // space violates RFC 2396
URI[] uris = StringUtils.stringToURI(paths);

// after
String[] paths = {new File("/data/my files/tool.jar").toURI().toString()}; // file:/data/my%20files/tool.jar
URI[] uris = StringUtils.stringToURI(paths);
Defensive patterns

Strategy: validation

Validate before calling

for (String s : paths) {
  try {
    new URI(s); // pre-parse: surfaces exact error position
  } catch (URISyntaxException e) {
    throw new IllegalArgumentException("Bad URI in config: '" + s + "'", e);
  }
}
URI[] uris = StringUtils.stringToURI(paths);

Try / catch

try { StringUtils.stringToURI(paths); } catch (IllegalArgumentException e) { URISyntaxException cause = (URISyntaxException) e.getCause(); /* cause.getIndex() locates the bad character */ }

Prevention

When it happens

Trigger: stringToURI over config values such as job cache files or distributed-cache entries containing 'C:\dir with spaces\x.jar', 'file:/data/my files/a.jar', or hand-concatenated scheme+path strings with unescaped characters.

Common situations: Windows paths with drive letters and spaces pasted into configs; raw filesystem paths used where URI syntax is expected; configs migrated between Linux and Windows clusters; typos like 'hdfs:/ /host:8020'.

Related errors


AI-assisted analysis of apache/hadoop@2add963021 (2026-08-22). Data as JSON: /api/errors/f2df304b4a2f01ef. Report an issue: GitHub.