Blankj/AndroidUtilCode · error · IllegalArgumentException

segment of <{segment}> is illegal

Error message

segment of <{segment}> is illegal

What it means

PathUtils.getLegalSegment() is called internally by PathUtils.join() to extract the non-separator portion of a child path segment. It throws IllegalArgumentException if the segment consists entirely of separator characters (File.separatorChar), meaning no valid path characters were found. On Linux/Android the separator is '/'; on Windows it would be '\'.

Source

Thrown at lib/utilcode/src/main/java/com/blankj/utilcode/util/PathUtils.java:65

        return newPath;
    }

    private static String getLegalSegment(String segment) {
        int st = -1, end = -1;
        char[] charArray = segment.toCharArray();
        for (int i = 0; i < charArray.length; i++) {
            char c = charArray[i];
            if (c != SEP) {
                if (st == -1) {
                    st = i;
                }
                end = i;
            }
        }
        if (st >= 0 && end >= st) {
            return segment.substring(st, end + 1);
        }
        throw new IllegalArgumentException("segment of <" + segment + "> is illegal");
    }

    /**
     * Return the path of /system.
     *
     * @return the path of /system
     */
    public static String getRootPath() {
        return getAbsolutePath(Environment.getRootDirectory());
    }

    /**
     * Return the path of /data.
     *
     * @return the path of /data
     */
    public static String getDataPath() {
        return getAbsolutePath(Environment.getDataDirectory());

View on GitHub (pinned to 7b4caf9e54)

Solutions

  1. Sanitize the child path before calling join: trim leading/trailing separators and validate it contains at least one non-separator character.
  2. Use a null/empty check plus separator stripping: child = child.replaceAll("^/+|/+$", "").
  3. If child may be separator-only, short-circuit: if (child.replaceAll(String.valueOf(File.separatorChar), "").isEmpty()) return parent.
  4. Consider using java.nio.file.Paths or java.io.File constructors instead of manual string joins for robust path building.

Example fix

// before
String path = PathUtils.join("/data/app", "///"); // throws

// after
String child = "///";
if (child != null && !child.replace("/", "").isEmpty()) {
    String path = PathUtils.join("/data/app", child);
} else {
    // handle invalid child
}
Defensive patterns

Strategy: validation

Validate before calling

// Sanitize child path before calling join
String child = getUserInput();
if (child != null && !child.trim().isEmpty()) {
    // Remove leading/trailing separators
    child = child.replaceAll("^[" + File.separator + "]+|[" + File.separator + "]+$", "");
    if (!child.isEmpty()) {
        String path = PathUtils.join(parent, child);
    }
}

Type guard

static boolean isValidPathSegment(String segment) {
    if (segment == null || segment.isEmpty()) return false;
    char sep = File.separatorChar;
    for (char c : segment.toCharArray()) {
        if (c != sep) return true;
    }
    return false;
}

Try / catch

try {
    String path = PathUtils.join(parent, child);
} catch (IllegalArgumentException e) {
    // Child segment was all-separator chars or invalid
    // Fall back to parent only or sanitize and retry
    String sanitized = child.replaceAll("/", "");
    if (!sanitized.isEmpty()) {
        path = PathUtils.join(parent, sanitized);
    } else {
        path = parent;
    }
}

Prevention

When it happens

Trigger: Calling PathUtils.join(parent, child) where child is a string composed solely of separator characters — e.g., join("/data", "///") or join("/data", "/"). Also triggered by join("/data", "") if TextUtils.isEmpty doesn't catch it (it does catch empty, but whitespace-only non-empty strings like " " still pass through and contain a non-separator char, so this specifically targets all-separator strings).

Common situations: Building paths from user input or config values that contain trailing/leading/duplicate separators; string concatenation that produces separator-only segments; null-safe defaults that resolve to separator characters.

Related errors


AI-assisted analysis of Blankj/AndroidUtilCode@7b4caf9e54 (2026-08-14). Data as JSON: /api/errors/b4fd5dffdd7067ac. Report an issue: GitHub.