Site icon TestingDocs.com

Command Line Arguments

Overview

This tutorial will teach you how to handle command-line arguments in a Java program. The command line argument is the argument/data passed to the program when we run it.

The arguments are stored in a String array called args that are passed to the Java main() method.

public static void main(String[] args)

Java Program Example

The command line arguments that are passed from the console are stored in a String array and can be retrieved in the Java program.

Let’s write a simple Java program to understand the concept. The following program will print the command line arguments each in a separate line.

/*
 * Command line arguments demo
 * Java Tutorials - www.TestingDocs.com
 */

public class JavaApplication {

	public static void main(String[] args) {
		for (int i = 0; i < args.length; i++) {
			System.out.println(args[i]);
		}
	}
}


In Eclipse, Run >> Run Configurations… choose the program and click the arguments tab to provide the Program arguments.

IntelliJ IDE

In IntelliJ IDE we can set the program arguments for the Java application in the run configuration.

Run >> Edit Configurations >> Application >> Program arguments

 

That’s it.

Java Tutorials

Java Tutorial on this website:

https://www.testingdocs.com/java-tutorial/

For more information on Java, visit the official website :

https://www.oracle.com/java/

Exit mobile version