Command line arguments are aggregated into an String[] in Java and passed into your main un-processed.

public static void main(final String[] args) { // code }

With your example input of D:/input a.conf,b.conf,c.conf D:/output the value of args becomes ["D:/input", "a.conf,b.conf,c.conf", "D:/output"] where the [] delimit an array of String.

You have two options:

  1. manually parse this args array and pull it apart manually and process each element yourself. In this case you need to Arrays.asList(args[2].split(",")); the second argument to get it into a List<String> as you desire.
  2. Use something like JSAP ( Java Simple Argument Parser ) and let a well tested and mature library parse, validate and format the input for you. Specifically it can parse the second argument you are passing in for you.

Needless to say, I prefer the second option because the first is just brittle busy work.

Answer from user177800 on Stack Overflow
🌐
Oracle
docs.oracle.com › javase › tutorial › essential › environment › cmdLineArgs.html
Command-Line Arguments (The Java™ Tutorials > Essential Java Classes > The Platform Environment)
The Echo example displays each of its command-line arguments on a line by itself: public class Echo { public static void main (String[] args) { for (String s: args) { System.out.println(s); } } } The following example shows how a user might run Echo. User input is in italics. ... Note that the application displays each word — Drink, Hot, and Java — on a line by itself.
🌐
Baeldung
baeldung.com › home › java › core java › command-line arguments in java
Command-Line Arguments in Java | Baeldung
December 27, 2025 - The code above expects the file path as a command-line argument. If no file path is provided, it throws an exception and prints the usage instructions instead of attempting to read the file. Let’s place a file named hello.txt in our resources folder and pass its absolute path as an argument in our IDE run configuration: "/home/baeldung/tutorials/core-java-modules/core-java-lang/src/main/resources/hello.txt"
🌐
Programiz
programiz.com › java-programming › command-line-arguments
Java Command-Line Arguments
class Main { public static void main(String[] args) { for(String str: args) { // convert into integer type int argument = Integer.parseInt(str); System.out.println("Argument in integer form: " + argument); } } } Let's try to run the program through the command line. // compile the code javac Main.java // run the code java Main 11 23
🌐
GeeksforGeeks
geeksforgeeks.org › java › command-line-arguments-in-java
Command Line Arguments in Java - GeeksforGeeks
If no arguments are given (e.g., java GFG), it throws ArrayIndexOutOfBoundsException since args is empty. Command-line arguments in Java are space-separated values passed to the main(String[] args) method.
Published   May 28, 2026
🌐
University of Pennsylvania
cis.upenn.edu › ~bcpierce › courses › 629 › papers › Java-tutorial › java › cmdLineArgs › cmdLineArgs.html
Command-Line Arguments
You should observe several conventions when accepting and processing command-line arguments with a Java application. Many applications that accept command-line arguments allow the user to specify various combinations of arguments in various orders. For example, the UNIX command that prints the contents of a directory -- the ls utility program -- accepts optional arguments that determine which file attributes to list and the order in which to list the files.
🌐
DigitalOcean
digitalocean.com › community › tutorials › command-line-arguments-in-java
Command Line Arguments in Java | DigitalOcean
August 3, 2022 - We have to pass the arguments as space-separated values. $ java com/journaldev/examples/CommandLineArguments.java "A" "B" "C" Number of Command Line Argument = 3 Command Line Argument 0 is A Command Line Argument 1 is B Command Line Argument 2 is C $ java com/journaldev/examples/CommandLineArguments.java 1 2 3 Number of Command Line Argument = 3 Command Line Argument 0 is 1 Command Line Argument 1 is 2 Command Line Argument 2 is 3 $
🌐
Whitman College
whitman.edu › mathematics › java_tutorial › java › cmdLineArgs › cmdLineArgs.html
Command Line Arguments
C:\> java Sort friends.txt In the Java language, when you invoke an application, the runtime system passes the command line arguments to the application's main method via an array of Strings. Each String in the array contains one of the command line arguments.
Find elsewhere
🌐
TechVidvan
techvidvan.com › tutorials › java-command-line-arguments
Java Command Line Arguments with Examples - TechVidvan
April 9, 2020 - For example – java CommandLine TechVidvan Java Tutorial · Press Enter and you will get the desired output. After performing the above steps, we will get the following output: ... We can also display a single argument in the command prompt. Following code shows this example: //Display one command-line argument.
🌐
Tutorialspoint
tutorialspoint.com › java › java-command-line-args.htm
Java - Command Line Arguments
The Java Virtual Machine (JVM) encapsulates these inputs into the args[] array. We can check the number of arguments passed using args.length. In case no command line argument is present, then this array is empty.
Top answer
1 of 10
83

Use the Apache Commons CLI library commandline.getArgs() to get arg1, arg2, arg3, and arg4. Here is some code:



    import org.apache.commons.cli.CommandLine;
    import org.apache.commons.cli.Option;
    import org.apache.commons.cli.Options;
    import org.apache.commons.cli.Option.Builder;
    import org.apache.commons.cli.CommandLineParser;
    import org.apache.commons.cli.DefaultParser;
    import org.apache.commons.cli.ParseException;

    public static void main(String[] parameters)
    {
        CommandLine commandLine;
        Option option_A = Option.builder("A")
            .required(true)
            .desc("The A option")
            .longOpt("opt3")
            .build();
        Option option_r = Option.builder("r")
            .required(true)
            .desc("The r option")
            .longOpt("opt1")
            .build();
        Option option_S = Option.builder("S")
            .required(true)
            .desc("The S option")
            .longOpt("opt2")
            .build();
        Option option_test = Option.builder()
            .required(true)
            .desc("The test option")
            .longOpt("test")
            .build();
        Options options = new Options();
        CommandLineParser parser = new DefaultParser();

        String[] testArgs =
        { "-r", "opt1", "-S", "opt2", "arg1", "arg2",
          "arg3", "arg4", "--test", "-A", "opt3", };

        options.addOption(option_A);
        options.addOption(option_r);
        options.addOption(option_S);
        options.addOption(option_test);

        try
        {
            commandLine = parser.parse(options, testArgs);

            if (commandLine.hasOption("A"))
            {
                System.out.print("Option A is present.  The value is: ");
                System.out.println(commandLine.getOptionValue("A"));
            }

            if (commandLine.hasOption("r"))
            {
                System.out.print("Option r is present.  The value is: ");
                System.out.println(commandLine.getOptionValue("r"));
            }

            if (commandLine.hasOption("S"))
            {
                System.out.print("Option S is present.  The value is: ");
                System.out.println(commandLine.getOptionValue("S"));
            }

            if (commandLine.hasOption("test"))
            {
                System.out.println("Option test is present.  This is a flag option.");
            }

            {
                String[] remainder = commandLine.getArgs();
                System.out.print("Remaining arguments: ");
                for (String argument : remainder)
                {
                    System.out.print(argument);
                    System.out.print(" ");
                }

                System.out.println();
            }

        }
        catch (ParseException exception)
        {
            System.out.print("Parse error: ");
            System.out.println(exception.getMessage());
        }
    }

2 of 10
35

You could just do it manually.

NB: might be better to use a HashMap instead of an inner class for the opts.

/** convenient "-flag opt" combination */
private class Option {
     String flag, opt;
     public Option(String flag, String opt) { this.flag = flag; this.opt = opt; }
}

static public void main(String[] args) {
    List<String> argsList = new ArrayList<String>();  
    List<Option> optsList = new ArrayList<Option>();
    List<String> doubleOptsList = new ArrayList<String>();

    for (int i = 0; i < args.length; i++) {
        switch (args[i].charAt(0)) {
        case '-':
            if (args[i].length < 2)
                throw new IllegalArgumentException("Not a valid argument: "+args[i]);
            if (args[i].charAt(1) == '-') {
                if (args[i].length < 3)
                    throw new IllegalArgumentException("Not a valid argument: "+args[i]);
                // --opt
                doubleOptsList.add(args[i].substring(2, args[i].length));
            } else {
                if (args.length-1 == i)
                    throw new IllegalArgumentException("Expected arg after: "+args[i]);
                // -opt
                optsList.add(new Option(args[i], args[i+1]));
                i++;
            }
            break;
        default:
            // arg
            argsList.add(args[i]);
            break;
        }
    }
    // etc
}
🌐
Edureka
edureka.co › blog › java-command-line-argument
Java Command Line Arguments With Examples | Java Tutorial | Edureka
August 27, 2024 - This article covers the concept of Java command line arguments in detail with various examples to show how you can use the command line arguments in Java.
🌐
TutorialsPoint
tutorialspoint.com › Java-command-line-arguments
Java program to display command-line arguments
November 7, 2024 - Use a loop to iterate over the args array and print each command-line argument. Display the index and the value of each argument. Java Program to display command-line arguments.
🌐
CodeGym
codegym.cc › java blog › core java › java command line arguments
Java command line arguments
February 14, 2025 - The method of accessing the command line arguments in java is very straightforward. To use these arguments within our java code is simple. They are stored as an array of Strings passed to the main(). It is mostly named as args.
🌐
Codemistic
codemistic.github.io › java › command-line-arguments-java.html
Command-Line Arguments in Java| Java Tutorials | CodeMistic
class CommandLineExample{ public static void main(String args[]){ System.out.println("Your first argument is: "+args[0]); } } compile by > javac CommandLineExample.java run by > java CommandLineExample sonoo
🌐
CS156
ucsb-cs156.github.io › topics › java › java_command_line_arguments.html
Java: Command Line Arguments | CS156 - GitHub Pages
If you run java Main foo bar fum then args.length is 3, args[0] is "foo", args[1] is "bar", and args[2] is "fum" Command line args come in as Strings. If you want an int, use Integer.parseInt as in this example from Oracle’s tutorial · int firstArg; if (args.length > 0) { try { firstArg = Integer.parseInt(args[0]); } catch (NumberFormatException e) { System.err.println("Argument" + args[0] + " must be an integer."); System.exit(1); } }
🌐
Scientech Easy
scientecheasy.com › home › blog › command line arguments in java
Command Line Arguments in Java - Scientech Easy
February 15, 2025 - For example, suppose that we entered the following command to run CommandLine program from the command prompt, as: ... This command has three command-line arguments beyond the java CommandLine command: hello, 10, and world.
🌐
DEV Community
dev.to › mohamad_mhana › java-program-with-command-line-arguments-5bb7
Java Program with Command-Line Arguments - DEV Community
September 29, 2025 - java Main 5 0 4 args[0] = "5" // ... array and calls Main.main(args) automatically. ... All command-line arguments are Strings, even numbers....
Top answer
1 of 1
2

How to pass args to main() in command line in Java?

First, your command line is incorrect.

Copy$ java -Dlog4j.configuration=logging.properties -classpath ${class_path} \
       --year="2021" --month="2" --day="2" --customerId=1234567

should be

Copy$ java -Dlog4j.configuration=logging.properties -classpath ${class_path} \
       ${class_name} --year="2021" --month="2" --day="2" --customerId=1234567

where ${class_name} is the full name of the class containing your entry point method; e.g. com.acme.myapp.Main or something. See https://stackoverflow.com/a/18093929/139985 for more details.

Once you have corrected the above, the args will be delivered in the args parameter as a value equivalent to:

Copynew String[]{"--year=2021", "--month=2", "--day=2", "--customerId=1234567"}

Notice that the shell will have removed the original quotes. (If you want to stop that then they need to be quoted or escaped on command line. It happens before Java even sees the arguments.)

It is now your problem to turn those strings into something that your program can understand. There are two approaches:

  • Implement the argument parsing in custom Java code; e.g. using String.split, Integer.parseInt and so on.

  • Find and use Java command line parser library. A Google search for java command line parser will give you a lot of candidates.

If you want to conform to an existing set of conventions (e.g. https://www.gnu.org/prep/standards/html_node/Command_002dLine-Interfaces.html), look for a library that handles this.

(Note that the standard Java SE libraries don't include command line argument parsing functionality. I guess the reason is OS command line argument conventions are sufficiently diverse that a usable portable solution is not feasible.)

🌐
W3Schools Blog
w3schools.blog › home › java command line arguments
Java Command Line Arguments - W3Schools.blog
August 24, 2014 - package com.w3schools; public class ... 20 Argument at 1 Position: 30 Argument at 2 Position: 40 · In the Package Explorer, right-click on your Java class like “CommandLineArgumentsTest”....