stanfordnlp/CoreNLP · error · IllegalArgumentException
Need an even number of arguments but there were " +…
Error message
Need an even number of arguments but there were " + args.length
What it means
PropertiesUtils.asProperties(String... args) builds a Properties object from alternating key/value strings, so it requires an even number of arguments. If an odd count is passed it throws IllegalArgumentException immediately, since the last key would have no value. This is a fail-fast guard on command-line style property arguments.
Solutions
- Count and pair your arguments; add the missing value for the last key
- Quote argument values containing spaces so they stay a single token
- Validate args.length % 2 == 0 in your main before calling asProperties
- Print usage/help when the argument count is wrong
Example fix
// before
Properties p = PropertiesUtils.asProperties("-filelist"); // odd
// after
Properties p = PropertiesUtils.asProperties("-filelist", "input.txt"); Defensive patterns
Strategy: validation
Validate before calling
if (args.length % 2 != 0) {
throw new IllegalArgumentException("Expected key/value pairs; got " + args.length
+ " args (dangling key: " + args[args.length - 1] + ")");
} Try / catch
try {
Properties p = PropertiesUtils.asProperties(args);
} catch (IllegalArgumentException e) {
if (e.getMessage().startsWith("Need an even number")) {
printUsage("Each key needs a value; missing value for last argument.");
System.exit(2);
}
throw e;
} Prevention
- Quote argument values containing spaces in shell scripts
- Validate raw command-line args before passing them through to asProperties
- Prefer explicit Properties files or builders over varargs key/value pairs for many options
- Print usage text when argument count is odd
When it happens
Trigger: Calling PropertiesUtils.asProperties("key1", "val1", "key2") — any invocation where args.length is odd, typically from a main() passing through raw command-line arguments with a missing value.
Common situations: User forgets a value on the command line (e.g. '-output' with no filename); a value containing spaces was not quoted so it shifted the argument positions; shell stripping an empty quoted argument.
Understand the failure class
Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.
Related errors
- Unknown properties: " + names
- Unknown property: " + names.iterator().next()
- args: treebankPath trainNums testNums
- argsToProperties could not read properties file: " + file
- Array lengths don't match
AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10).
Data as JSON: /api/errors/4415b3fb042432fb.
Report an issue: GitHub.
Appendix: source
Thrown at src/edu/stanford/nlp/util/PropertiesUtils.java:54
return ! (value.equals("false") || value.equals("no") || value.equals("off"));
}
public static boolean hasPropertyPrefix(Properties props, String prefix) {
for (Object o : props.keySet()) {
if (o instanceof String && ((String) o).startsWith(prefix)) return true;
}
return false;
}
/** Create a Properties object from the passed in String arguments.
* The odd numbered arguments are the names of keys, and the even
* numbered arguments are the value of the preceding key
*
* @param args An even-length list of alternately key and value
*/
public static Properties asProperties(String... args) {
if (args.length % 2 != 0) {
throw new IllegalArgumentException("Need an even number of arguments but there were " + args.length);
}
Properties properties = new Properties();
for (int i = 0; i < args.length; i += 2) {
properties.setProperty(args[i], args[i + 1]);
}
return properties;
}
/** Convert from Properties to String. */
public static String asString(Properties props) {
try {
StringWriter sw = new StringWriter();
props.store(sw, null);
return sw.toString();
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}View on GitHub (pinned to 1b7edd19c4)