Java command-line arguments enable passing arguments into a program during its execution. These arguments, provided via the console, can be accepted within the Java program and utilized as input values.
A String array is provided as inputs in the main method, allowing programmers to utilize various values without the need to modify the code.
This feature offers a practical method to examine how a program behaves with various inputs. Users can input any number of arguments (e.g., 1, 2, 3, and so forth) through the command line interface.
Why Use Command-Line Arguments?
Using command-line arguments provides a versatile method for supplying input values to Java applications without requiring modifications to the source code. This approach is especially beneficial in situations such as automation, where scripts must execute with varying inputs, or in configuration management, where settings differ across environments like development, testing, and production.
Developers can enhance the dynamism and reusability of programs by utilizing test cases to efficiently assess different input scenarios. Instead of embedding values directly into the code, developers have the flexibility to provide them externally. This approach results in code that is more organized and simpler to maintain.
Example of Command Line Argument
In this instance, we are accepting a singular argument and displaying it. To execute this Java code, it is necessary to provide a minimum of one argument through the command line.
class Main {
public static void main(String args[]) {
System.out.println("Your first argument is: "+args[0]);
}
}
Output:
Your first argument is: Andrew
Run the program above using the commands provided below.
compile by > javac Main.java
run by > java Main Andrew
How to Handle Missing Arguments?
To prevent an ArrayIndexOutOfBoundsException, it is essential to verify the length of the args array before accessing arguments that were not provided:
if (args.length > 0) {
System.out.println("First argument: " + args[0]);
} else {
System.out.println("No arguments provided.");
}
Implementing the code snippet above can help prevent crashes and enhance the robustness of your codebase.
Converting Argument Types
Command-line arguments are initially received as strings. When numerical values like integers, floats, or booleans are required for operations, parsing is necessary.
int num = Integer.parseInt(args[0]);
double value = Double.parseDouble(args[1]);
boolean flag = Boolean.parseBoolean(args[2]);
Ensure proper handling of exceptions such as NumberFormatException when converting input values.
Example: Printing Multiple Values Using Command Line Arguments
In this instance, we are displaying all the parameters provided through the command line. To achieve this, we iterated through the array utilizing a for loop.
class Main {
public static void main(String args[]) {
for(int i=0;i<args.length;i++)
System.out.println(args[i]);
}
}
Output:
Andrew
25
University
of
California
Run the program by employing the commands provided below.
compile by > javac Main.java
run by > java Main Andrew 25 University of California
Important Points to Remember
- While running a Java program in the command prompt, arguments are provided in the console.
- Arguments are added to the String args array used by the main method.
- The first argument is in args[0], the second in args[1] and, so on.
- Trying to use a non-existent index will cause an ArrayIndexOutOfBoundsException to be thrown.
- The number of arguments sent can be checked with args.length.
- We can take advantage of loops (for example, a for loop) to loop through and print out all the arguments.
- Every command-line argument, regardless of being a number, is still regarded as a string.
Conclusion
Java's command line arguments offer a versatile and effective way of inputting data into a program during its execution. This method allows developers to customize the behavior of their programs without the need to modify the source code. Leveraging command line arguments in Java enhances the dynamism, testability, and automation capabilities of Java applications.
Java Command Line Arguments MCQs
- Which method receives command-line arguments in Java?
- run
- main
- start
- init
Explanation: These arguments are received in the main method as a String args array.
- What is the data type of command-line arguments in Java?
- String
- Array
- String
- char
In the main method, a String array is provided as inputs for programmers to utilize various values without modifying the code.
What is the maximum number of arguments that can be transmitted to the main method?
Unlimited
Explanation: We can pass N (1, 2, 3, and so on) numbers of arguments from the command prompt.
- Can command-line arguments be automatically converted to integers?
- Only in Java 11+
- Depends on JVM
Explanation: All command-line arguments are passed as strings. If logic requires integers, floats, or booleans, we must parse them.
- We can input command-line arguments at _________.
- Run Time
- Compile Time
- Both a and b
- None of the above
Command line arguments in Java offer a versatile and effective method for supplying input to a program while it is running.