Understand Command Line Arguments in java

In this example we are going to understand the command line arguments in java. We are going to write a very simple java class with the print statement and which will take one argument.

public class CmdArgs {

    public static void main(String[] args) {

        System.out.println("My name is: " + args[0]);
    }
}


In order to compile the above java class, go to the command prompt and navigate to the directory where the java class is stored. Now, issue the command to compile the java class as:
javac CmdArgs.java

Now, to run the java class, fire the following command.
java CmdArgs Amzi

The output will be something like this:

My name is: Amzi

No comments:

Post a Comment