apache/hadoop · error · HadoopIllegalArgumentException

"Unexpected argument " + args[nextArgIndex]

Error message

"Unexpected argument " + args[nextArgIndex]

What it means

While parsing arguments, LogLevel.CLI throws HadoopIllegalArgumentException for any token that is not one of the recognized flags -getlevel, -setlevel, or -protocol. The offending token is included in the message. This is strict prefix parsing: the very first unrecognized word aborts the whole command before any request is sent.

Source

Thrown at hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/log/LogLevel.java:155

          "Expect either -getlevel or -setlevel");
      }
    }

    public void parseArguments(String[] args) throws
        HadoopIllegalArgumentException {
      if (args.length == 0) {
        throw new HadoopIllegalArgumentException("No arguments specified");
      }
      int nextArgIndex = 0;
      while (nextArgIndex < args.length) {
        if (args[nextArgIndex].equals("-getlevel")) {
          nextArgIndex = parseGetLevelArgs(args, nextArgIndex);
        } else if (args[nextArgIndex].equals("-setlevel")) {
          nextArgIndex = parseSetLevelArgs(args, nextArgIndex);
        } else if (args[nextArgIndex].equals("-protocol")) {
          nextArgIndex = parseProtocolArgs(args, nextArgIndex);
        } else {
          throw new HadoopIllegalArgumentException(
              "Unexpected argument " + args[nextArgIndex]);
        }
      }

      // if operation is never specified in the arguments
      if (operation == Operations.UNKNOWN) {
        throw new HadoopIllegalArgumentException(
            "Must specify either -getlevel or -setlevel");
      }

      // if protocol is unspecified, set it as http.
      if (protocol == null) {
        protocol = PROTOCOL_HTTP;
      }
    }

    private int parseGetLevelArgs(String[] args, int index) throws
        HadoopIllegalArgumentException {

View on GitHub (pinned to 2add963021)

Solutions

  1. Correct the flag spelling — only -getlevel, -setlevel, and -protocol are accepted.
  2. Remove stray tokens before the flags; put generic options (e.g. -Dkey=value) at the front of the command where the framework handles them.
  3. Re-run with the exact usage shape: hadoop loglevel -getlevel <host:port> <classname> [-protocol (http|https)].

Example fix

# before
hadoop loglevel getlevel namenode:9870 org.apache.hadoop.ipc.Server

# after
hadoop loglevel -getlevel namenode:9870 org.apache.hadoop.ipc.Server
Defensive patterns

Strategy: validation

Validate before calling

Set<String> valid = Set.of("-getlevel", "-setlevel", "-protocol");
for (String a : args) {
  if (a.startsWith("-") && !valid.contains(a)) {
    throw new HadoopIllegalArgumentException("Unknown flag " + a + "; valid: " + valid);
  }
}
ToolRunner.run(new LogLevel.CLI(conf), args);

Prevention

When it happens

Trigger: Typo'd flags (e.g. 'getlevel' without the leading dash, '-getlevels', '-setniveau'); pasting extra positional words before the flags; leftover shell tokens (like a stray option from a copy-pasted command) reaching the parser.

Common situations: Transcribing commands from documentation and losing the dash; locale keyboards producing a different hyphen character; scripts appending debug flags in the wrong position (note: generic options like -D are consumed by GenericOptionsParser only at the front).

Related errors


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