How to Modify Java Command-Line Arguments
Learn how to modify Java command-line arguments. Java command-line arguments enable programmers to pass the arguments during the execution of the program.
Join the DZone community and get the full member experience.
Join For FreeHow do you check that your code is error-free, works in all situations, and handles all the edge cases?
Well, the simpler way is to check it against all the possible ranges of test cases, which can be really vast sometimes. Passing these test cases could be really hectic and time-consuming in such scenarios. We need some way that can get this done easily without much hassle and can be automated for bigger inputs. Command-line arguments in Java are one such solution to our problem to make our testing and debugging process easier.
In this article, we will discuss briefly what command-line arguments are, how they are passed, and how they work; but our main focus will be on how to modify Java command-line arguments.
// here args will hold the command line arguments
public static void main(String[] args) {
System.out.println("Hello World!");
// remaining code will go here
}
What Are Command-Line Arguments?
Java command-line arguments enable programmers to pass the arguments during the execution of the program. The programmers can pass these arguments directly from the console which will be accessed by the main()
method and can be used as inputs. We can also bypass the command-line arguments by passing values directly to the main()
method.
How Do Command-Line Arguments Works?
Command-line arguments that are provided while executing are wrapped up and supplied to args[]
. Args[]
is essentially an array of string that holds all the command-line arguments provided to it. Each argument will be stored at an index, starting from args[0]
all the way to args[n]
.
How Do You Pass Command-Line Arguments?
Command-line arguments are passed through command prompts or terminals. All the arguments passed through it will be converted and wrapped into args[]
array internally by JVM.
Now let’s see how to pass command-line arguments with the help of an example. Suppose we have a file with some Java code. The following steps will demonstrate how to pass command-line arguments in Java:
Save the Java program with the
.java
extension (e.g.Arguments.java
).- Open terminal/command prompt into the directory where the program is stored locally.
Compile the Java program to convert
.java
file to.class
fileCommand:
javac filename.java
(Here,javac Arguments.java
)
- Run the program and pass the arguments after the filename is separated by spaces.
- Command:
java filename argument1 argument2 …. argumentN
(Here, Java argumentsWelcome to this blog and Happy Learning
)
- Command:
How Do You Access Command-Line Arguments?
As discussed in the case above, let’s try to access all those passed command-line arguments in Java now. How do we do it?
Since these arguments are stored in the args[]
array, that means we can access them easily using args[i]
, where I specify an index that can vary from 0
to n
(total numbers of arguments passed).
Below code is the demonstration of the same:
class Arguments {
public static void main( String[] args ) {
System.out.println(" Hello World! ");
// args.length is used to get length of args array
System.out.println(" Total number of args: " + args.length);
// iterating over the args array to print the args if available
for (int i = 0; i < args.length; i++) {
System.out.println(" Arg " + i + ": " + args[i]);
}
}
}
Output
Hello World!
Total number of args: 7
Arg 0: Welcome
Arg 1: to
Arg 2: this
Arg 3: blog
Arg 4: and
Arg 5: Happy
Arg 6: Learning
How Do You Modify Command-Line Arguments?
Again, we can make use of args[i]
to get value at ith index, and then use assignment operator (=
), we can modify the command-line argument at ith index.
The below snippet will modify some of the arguments passed through the command line in Java.
args[1] = " Modified Command Line Argument 1 ";
args[3] = " Modified Command Line Argument 2 ";
args[5] = " Modified Command Line Argument 3 ";
Updated Output
Hello World!
Total number of args: 7
Arg 0: Welcome
Arg 1: Modified Command Line Argument 1
Arg 2: this
Arg 3: Modified Command Line Argument 2
Arg 4: and
Arg 5: Modified Command Line Argument 3
Arg 6: Learning
In a Nutshell
Java command-line arguments are a way of providing inputs to the program through a command prompt.
Command-line arguments are passed while executing the run command, arguments are passed immediately after the filename, separated by spaces.
Command-line arguments are eventually received by an array of strings (
args[]
) in the main function.We can access or modify the command-line arguments using
args[i]
and assignment operators just like we do in normal programming languages.
Opinions expressed by DZone contributors are their own.
Comments