Introduction
The command-line arguments in Java allow you to pass arguments during the execution of the program. The arguments passed from the console can be received in the java program & it can be used as an input. You can pass n numbers of arguments( 1,2,3,4----n) from the command prompt.
Example of command-line argument in java
In this example, we are receiving only one argument & printing it. To run this java program, you have to pass at least one argument from the command prompt like C:\>
class CmdLineArgu {
public static void main(String args[]) {
System.out.println("Your first argument is: " + args[0]);
}
}
Working of Command Line Argument
We are required to pass the arguments as space-separated values. We can pass strings and primitive data types such as int, double, float, char, etc as command-line arguments. These arguments convert into a string array & provided to the main() function as a string array argument.
When command-line arguments are applied to Java Virtual Machine, JVM, then JVM wraps these and supplies them to args[]. They are actually wrapped up in an args array by checking the length of args using the args.length functions.
Internally, the Java Virtual Machine wraps up these command line arguments into the args[ ] array that we can pass into the main() function. We can check these arguments using args.length(). JVM stores the first command line argument at args[0], second arguments at args[1], also third argument at args[2], and so on.
Consider a program in Java that accepts two parameters on the command line. If there are no command line arguments entered, the program should print error message and exit. The program should check whether the first file exists and is it an ordinary file. If it is so, then the contents of the first file should be copied to the second file. In case the first parameter is a directory, print message accordingly and exit. An appropriate error messages should be displayed at all points.
Note
The usage of try block is introduced in order to catch the IOException thrown so as to enable printing customized error messages
Source Code
import java.io.*;
class FileCheck {
public static void main(String args[]) {
if (args.length == 0 | | args.length == 1) {
System.out.println("Invalid Usage");
System.out.println("Usage: java <filename> file1 file2 ");
return;
}
File f4 = new File(args[0]);
File f1 = new File(args[1]);
try {
BufferedReader d = new BufferedReader(new FileReader(args[0]));
DataOutputStream o = new DataOutputStream(new FileOutputStream(args[1]));
String line;
if (f4.exists() && f4.isFile()) {
System.out.println("File exists and is an ordinary file");
System.out.println("Writing contents of " + f4 + "to" + args[1]);
while ((line = d.readLine()) != null) {
o.writeBytes(line + "\r\n");
}
}
o.close();
} catch (IOException e) {
if (f4.exists() && f4.isDirectory()) {
System.out.println("Name: " + f4 + " is a directory");
System.out.println("contents cannot be copied");
} else {
System.out.println("*******************************");
System.out.println(f4 + "does not exists");
System.out.println("*******************************");
}
}
}
}
Save the file as FileCheck.java. Compile and execute the program.
Summary
The information that we have passed after typing the name of the Java program during the program execution. These arguments stored as a Strings in a String array and passed to the main() function. We can use these command-line arguments as input in Java program.