Java Command Line Arguments: Dynamic Input for Your Programs
Learn about Java command line arguments, which enable you to pass inputs to your Java program during execution. These arguments are captured as a String
array in the main()
method, allowing developers to dynamically configure program behavior without altering the code. Discover how to utilize command line arguments effectively to enhance the flexibility and functionality of your Java applications.
Java - Command Line Arguments
Overview of Command Line Arguments
In Java, command line arguments provide a way to pass inputs to a Java program during execution. These arguments are typically passed from the console, and they are captured in the main()
method as a String
array. This mechanism allows developers to dynamically configure the behavior of a program without changing its code.
Passing & Accessing Command Line Arguments
Command line arguments are passed during the execution of the Java program. Here's the syntax:
javac Tester.java java Tester arg1 arg2 arg3
In the example above, three arguments (arg1
, arg2
, arg3
) are passed to the Tester
class. These arguments are stored in the args[]
array, which can be accessed inside the main()
method. If no arguments are passed, the array is empty, and its size can be checked using args.length
.
class Tester { public static void main(String[] args) { // Access command line arguments here } }
Benefits of Command Line Arguments
- Command line arguments enable dynamic parameterization of Java programs, making them versatile and interactive.
- They allow users to pass configuration parameters to batch processes or console-based applications.
- This approach enhances user input retrieval and manipulation in console-based applications.
Example of Single Command Line Argument
In this example, the program checks if exactly one argument is passed to represent a name. If no arguments or more than one argument are passed, an error message is displayed. Otherwise, the program prints the name with a salutation.
public class Tester { public static void main(String[] args) { // Check if exactly one argument is passed if(args.length == 1) { String name = args[0]; System.out.println("Welcome " + name + "!"); } else { System.out.println("Invalid Command line argument(s) passed."); } } }
Output
D:\test>javac Tester.java D:\test>java Tester Invalid Command line argument(s) passed. D:\test>java Tester Mahesh Welcome Mahesh!
Example of Multiple Command Line Arguments
This example checks if exactly two arguments are passed to represent a name and age. The second argument is parsed as an integer to represent age. If no arguments or more than two arguments are passed, an error message is displayed. Otherwise, the program prints the name and age.
public class Tester { public static void main(String[] args) { // Check if exactly two arguments are passed if(args.length == 2) { String name = args[0]; // Parse the age as int int age = Integer.parseInt(args[1]); System.out.println("Name: " + name + ", age: " + age); } else { System.out.println("Invalid Command line argument(s) passed."); } } }
Output
D:\test>javac Tester.java D:\test>java Tester Invalid Command line argument(s) passed. D:\test>java Tester Mahesh 40 Name: Mahesh, age: 40
Conclusion
Java command-line arguments are a powerful feature for creating parameterized programs that can accept inputs dynamically. They allow users to control program behavior at runtime by passing arguments to the main()
method, without needing to recompile the code. Command-line arguments are supported by various IDEs, making them easy to implement and use in different execution environments.