Pages

How to pass arguments to Java Program

How to pass arguments to Java Program?

We can pass arguments to the Java program and can use it anywhere throughout the execution of the program.

Demo on pass arguments to java program:
public class EnvVariables {
    public static void main(String args[]) {
        for (int i =0; i < args.length; i++) {
            System.out.println(i + " argument= " + args[i]);
        }
    }
}

Run program using below commands:
>java EnvVariables chrome Firefox Edge
Then will get output like below.
Output:
0 argument= Chrome
1 argument= Firefox
2 argument= Edge

Here, we are passing our required arguments(chrome,Firefox,Edge) through the command line and args[] array will take those arguments in the program.
Please comment below to feedback or ask questions.

No comments:

Post a Comment

Please comment below to feedback or ask questions.