How do I parse command line arguments in Java?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In Java, command line arguments arrive as the String[] args parameter of main. For very small programs, manual parsing is enough, but for real CLI tools you usually want a library that can handle options, defaults, validation, and help output cleanly.
Start with String[] args
Every argument is initially just a string in order.
If you run:
then args[0] is first and args[1] is second.
This is enough for programs that only need positional parameters or one or two simple flags.
Manual parsing works for simple cases
For a small tool, you can scan the array yourself.
This is fine when the command line is small and fixed. Once options become numerous or interdependent, manual parsing becomes repetitive and error-prone.
Use a library for real CLI applications
Libraries such as picocli, Apache Commons CLI, and JCommander exist because robust CLI parsing has lots of edge cases. A parser library gives you:
- required and optional options
- help generation
- type conversion
- validation
- subcommands
picocli is a popular modern choice. A minimal example looks like this:
That is much easier to maintain than a growing manual switch statement once the CLI gets serious.
Decide between positional arguments and options
A good CLI usually separates:
- positional arguments for required ordered values
- named options for flags and optional values
For example, mytool input.txt output.txt is naturally positional. A verbosity switch or timeout value is better as --verbose or --timeout 10.
Good argument design matters as much as good parsing code. A confusing CLI remains confusing even if the parser library is excellent.
Common Pitfalls
- Treating all arguments as positional strings even when named options would make the CLI clearer.
- Writing manual parsing logic that does not validate missing option values or unknown flags.
- Forgetting that everything in
argsstarts as a string and needs explicit conversion. - Reimplementing help text and error formatting badly instead of using a parser library.
- Growing a simple manual parser far beyond the point where a CLI library would be easier to maintain.
Summary
- Java command line arguments arrive as
String[] argsinmain. - Manual parsing is fine for small simple tools.
- For real CLI applications, use a library such as picocli or Apache Commons CLI.
- Separate positional arguments from named options deliberately.
- The best parser choice depends on command complexity, not just personal preference.
Related reading
- How do I pass a class as a parameter in Java?
- How do I prevent the modification of a private field in a class?
- How do I print a double value without scientific notation using Java?
- How do I print my Java object without getting SomeType@2f92e0f4?
- How do I programmatically determine operating system in Java?
- How do I prove that Object.hashCode can produce same hash code for two different objects in Java?
- How do I remove repeated elements from ArrayList?
- How do I resolve ClassNotFoundException?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.