When you execute the program, you specify the class that contains the main method and then command line arguments. args[0] is the first command line argument. You need to provide it when you run the program.

java -cp . com.something.Main A B C

where A is args[0], B is args[1] and C is args[2].

Answer from Elliott Frisch on Stack Overflow
🌐
Caltech
courses.cms.caltech.edu › cs11 › material › java › donnie › java-main.html
Java Program Invocation and Command-Line Arguments
In Java, args[0] is the first command-line argument to the program, and the name of the program itself is not available.)
🌐
GeeksforGeeks
geeksforgeeks.org › java › command-line-arguments-in-java
Command Line Arguments in Java - GeeksforGeeks
Explanation: When the program is executed using java Hello Geeks at GeeksforGeeks, the JVM stores the three command-line arguments in the args array as args[0] = "Geeks", args[1] = "at", and args[2] = "GeeksforGeeks".
Published: September 28, 2026
🌐
Oracle
docs.oracle.com › javase › tutorial › essential › environment › cmdLineArgs.html
Command-Line Arguments (The Java™ Tutorials > Essential Java Classes > The Platform Environment)
To have Drink, Hot, and Java interpreted as a single argument, the user would join them by enclosing them within quotation marks. ... If an application needs to support a numeric command-line argument, it must convert a String argument that represents a number, such as "34", to a numeric value. Here is a code snippet that converts a command-line argument to an int: 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); } }
🌐
Medium
ahedbahri.medium.com › what-is-the-use-of-args-in-java-95a15c180e8c
What is the use of args in Java?. The interesting use of args in Java. | by Ahed Bahri | Medium
February 27, 2020 - public static void main (String[] args) {System.out.println(" The First argument "+args[0]); // AhedSystem.out.println(" The second argument "+args[1]); // 22System.out.println(" the first letter of the first argument "+args[0].charAt(0)); //ASystem.out.println(" the second letter of the first argument"+args[0].charAt(1)); //h// converting from string to integer int a = Integer.parseInt(args[1]); // a = 22int b = Integer.parseInt(args[2]); // b = 23int somme=a+b; // Somme = 22+23// for example this will be the result of a+bSystem.out.println("Somme : "+somme); // Somme : 45// converting from string to Floatfloat f =Float.parseFloat(args[2]); // f = 23.0System.out.println("Float : "+f); // Float : 23.0// This will also show you the argument caracter by caracter.
🌐
ThoughtCo
thoughtco.com › using-command-line-arguments-2034196
Using Command-Line Arguments in a Java Application
January 20, 2019 - When arguments are passed to a Java program, args[0] is the first element of the array (value1 above), args[1] is the second element (value2), and so on.
Find elsewhere
🌐
Medium
medium.com › @AlexanderObregon › processing-command-line-arguments-in-java-applications-200ba9b7f029
Processing Command-Line Arguments in Java Applications
March 14, 2025 - The args[] array will be empty (args.length == 0). Since Java does not automatically provide a default value, accessing args[0] would throw an ArrayIndexOutOfBoundsException.
🌐
Baeldung
baeldung.com › home › java › core java › check if command-line arguments are null in java
Check if Command-Line Arguments Are Null in Java | Baeldung
January 8, 2024 - public static void main(String[] args) { if (args.length > 0) { System.out.println(args[0]); } else { System.out.println("No command line arguments were provided."); } }
🌐
Emory
cs.emory.edu › ~cheung › Courses › 170 › Syllabus › 09 › command-args.html
The command line arguments of a Java program
Java program that prints the elements in the parameter variable args: Example Program: (Demo above code) &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp · Prog file: click here ·
🌐
Emory
mathcs.emory.edu › ~cheung › Courses › 170 › Syllabus › 09 › command-args.html
Emory
Java program that prints the elements in the parameter variable args: Example Program: (Demo above code) &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp · Prog file: click here ·
🌐
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.
Top answer
1 of 2
18

In some cases a program can be run in different ways and exhibit different behavior on how it is called. If you call vim as vi, it runs in a compatibility mode. Sometimes it is to try to maintain one version of several related programs - for example mailq and newaliases on many unix systems are a link to sendmail so that these programs stay in sync)


Java programs are typically invoked as:

% java -jar foo.jar args
% java Foo args

The first version is where you have a Manifest file that indicates the main class, the second version runs the main method in the class Foo found in the class path.

The information presented for Java is either a path to the jar or the name of the class being invoked.

The location of the jar isn't important enough to be something to code from (and was actually not part of the original spec). A Jar can be named anything really, and often includes version numbers. Whats more, there's no guarantee that the class was even stored in a .jar (it could have been extracted).

Invoking a Java application with -jar has only one way to enter it - the class defined in the Manifest. There's no renaming that can be done.

The other option, of invoking it with the class name points directly to the execution unit. Furthemore, it can't be named multiply - you can't have Bar.class be the code for class Foo it just doesn't work that way.

This should show that there's really no point to passing the information of argv[0] in the C sense to a Java application - its either going to be java, meaningless and arbitrary, or the name of the class that is being invoked (that you are already executing code out of (you could do something like getClass().getEnclosingClass().getName() if you were desperate...)).

There is a point here, you can define multiple Main methods in classes in a .jar or on the class path. And you could have them behave differently just as if there was a series of if statements based on what argv[0] was.

I have in the past had code akin to java -cp Foo.jar com.me.foo.Test which invoked the Test class's Main method rather than the one defined in the one defined in the Manifest.

2 of 2
-4

actually there's no benefit with it, it really depends on the syntax of the programming language you are using if it is 0-based or 1-based. the variable (you refer to as filename) also depends on the language, it can be different in other languages just follow the correct syntax of the language you are using.