Java Command-Line Arguments
Command-line arguments in Java allow you to pass data to a Java program at the time of execution from the command prompt. They are commonly used when you need to provide input to a program without having to modify the source code. Command-line arguments are often employed in console-based applications for tasks like passing configuration settings, input files, or user options.
Command-line arguments in Java are arguments that are passed to a Java program when it is executed from the command line or terminal. These arguments are stored in an array of String
objects, which are passed to the main()
method of the Java program.
The main()
method in Java always has the following signature:
public static void main(String[] args)
Here, args
is a String array that holds the command-line arguments.
To pass command-line arguments, you provide them after the class name when running the Java program from the command prompt.
For example, if you run the program like this:
java MyProgram arg1 arg2 arg3
The values arg1
, arg2
, and arg3
are passed to the main()
method in the args
array.
Inside the main()
method, the args
array contains the command-line arguments. The index of the array corresponds to the position of the argument. For example:
args[0]
will contain the first argument (arg1
)args[1]
will contain the second argument (arg2
)If no arguments are passed, the args
array will be empty (its length will be zero).
Here’s a simple Java program that prints all the command-line arguments passed to it:
public class CommandLineArgsExample {
public static void main(String[] args) {
// Check if arguments were passed
if (args.length > 0) {
System.out.println("Command-Line Arguments Passed:");
// Iterate over the arguments array and print each argument
for (String arg : args) {
System.out.println(arg);
}
} else {
System.out.println("No command-line arguments were passed.");
}
}
}
args.length > 0
).args
array and prints each one.To run this program from the command line, you would use the following syntax:
java CommandLineArgsExample Hello World 123
Output:
Command-Line Arguments Passed:
Hello
World
123
Since command-line arguments are always passed as String
values, you may need to convert them into other data types (such as int
, double
, etc.) depending on the requirements of your program.
public class CommandLineArgsConversionExample {
public static void main(String[] args) {
if (args.length == 2) {
try {
// Convert arguments to integers
int num1 = Integer.parseInt(args[0]);
int num2 = Integer.parseInt(args[1]);
// Perform a simple addition
int sum = num1 + num2;
System.out.println("Sum of " + num1 + " and " + num2 + " is: " + sum);
} catch (NumberFormatException e) {
System.out.println("Please provide valid integers.");
}
} else {
System.out.println("Please provide exactly two integers.");
}
}
}
int
values using Integer.parseInt()
.NumberFormatException
and prints an error message.To run the program:
java CommandLineArgsConversionExample 5 10
Output:
Sum of 5 and 10 is: 15
If the user provides invalid arguments (e.g., java CommandLineArgsConversionExample 5 abc
), the program will catch the exception and print:
Please provide valid integers.
Command-line arguments are particularly useful in scenarios such as:
import java.io.File;
public class FileProcessor {
public static void main(String[] args) {
if (args.length == 1) {
File file = new File(args[0]);
if (file.exists() && file.isFile()) {
System.out.println("File found: " + file.getName());
// Add file processing logic here (e.g., read file contents)
} else {
System.out.println("File not found or invalid file path.");
}
} else {
System.out.println("Please provide the path to the file.");
}
}
}
To run the program with a file path argument:
java FileProcessor /path/to/file.txt
Output (if the file exists):
File found: file.txt
Output (if the file does not exist):
File not found or invalid file path.
You can also handle multiple command-line arguments by accessing them through the args
array. For example, you might want to pass multiple parameters like file paths, user credentials, or other values.
public class MultiArgExample {
public static void main(String[] args) {
if (args.length == 3) {
String username = args[0];
String password = args[1];
String filePath = args[2];
System.out.println("Username: " + username);
System.out.println("Password: " + password);
System.out.println("File Path: " + filePath);
} else {
System.out.println("Please provide username, password, and file path.");
}
}
}
java MultiArgExample user123 mypassword /path/to/file.txt
Output:
Username: user123
Password: mypassword
File Path: /path/to/file.txt