What are the command line arguments in Java?

The command line arguments in java are the arguments passed to the program from the console. The command line argument in java is the information passed to the program at the time of running the program. It is the argument passed through the console when the program is run. The command line argument is the data that is written right after the program’s name at the command line while executing the program. The arguments passed to the java program through this command line can be received by the program as an input and used within the code.

How to access java command line arguments?

The method of accessing the command line arguments in java is very straightforward. To use these arguments within our java code is simple. They are stored as an array of Strings passed to the main(). It is mostly named as args. Have a look at the common header in the snippet below.

public static void main(String[] args){…}

Example

Let’s look at an example explained in detail below.

// Program to check for command line arguments
public class Example {

	public static void main(String[] args) {
		
		// check if the length of args array is < 0
		if (args.length <= 0) {
			System.out.println("No command line arguments found.");

		} else {
			System.out.println("The first command line argument is: " + args[0]);

			System.out.println("All of the command line arguments are: ");

			// iterating the args array and printing all of the command line arguments
			for (String index : args)
				System.out.println(index);
		}
	}
}

Execution

To execute the program, pass the arguments on the command line in the following way. We’re using IntelliJ IDE here, you can use any of your choice. For IntelliJ, choose the option, “Run” → “Edit Configurations”. Java command line arguments - 1Next, go to the “Program arguments” tab that is second on the available tabs. You can enter your arguments in that block available, click “Ok” and then “Run”. Java command line arguments - 2For the same output as this program, use the text below.
My name is Andrew.

Output

The first command line argument is: My All of the command line arguments are: My name is Andrew.

Explanation

In the above snippet of code, we have passed the command line arguments My name is Andrew. while executing the code after the program name. The arguments are then accessed in our code through the args variable.

By now, you've got a good grip on how to pass command-line arguments in IntelliJ. But what about Eclipse or NetBeans? And did you know you can pass more than just simple strings? Let’s explore these topics to round out your understanding of Java command-line arguments. Ready? Let's go!

Passing Command-Line Arguments in Eclipse and NetBeans

If you’re working in Eclipse, open your Run Configurations by clicking on “Run” in the toolbar, then select “Run Configurations…” from the dropdown. Look for the “Arguments” tab, and type your arguments in the “Program arguments” field. That’s it! When you run your program, Eclipse will pass those arguments to your main method. NetBeans has a similar process: right-click on your project, select “Properties,” then find “Run.” In the “Arguments” field, type your command-line parameters, and click OK. The next time you run your program, NetBeans will hand off those arguments to your code.

Even though the steps differ slightly between IDEs, the concept stays the same: you supply the arguments in a special field, and the IDE appends them when running your main method. Easy, right?

Types of Data You Can Pass as Arguments

One big misunderstanding is thinking command-line arguments must always be strings. It's true that Java receives them as strings in the args array, but you can parse them into other data types. For example, you can convert them to integers using Integer.parseInt() or to doubles using Double.parseDouble(). As long as your program can interpret the string as a valid number or other type, you're good to go.

Imagine passing a set of numbers to your program, then summing them up. Just parse them to int or double, and you’re off to the races!

Passing Arguments Directly from the Command Line or Using a Jar File

Running your Java program outside of an IDE? No problem. If your compiled class is named MyProgram, and your arguments are arg1 arg2 arg3, simply open a terminal and type:

java MyProgram arg1 arg2 arg3

Your main method will receive those three strings in the args array. Or, if you have a jar file, you can run it like this:

java -jar MyProgram.jar arg1 arg2 arg3

As with an IDE, you’ll receive these arguments in args. You can parse them, concatenate them, do arithmetic with them—whatever your program requires. The sky's the limit.

Conclusion

By the end of this post, we hope you have got yourself familiarized with the command line arguments in Java. Keep practicing for a deeper command of the concept. Till then, keep growing and keep shining!