User Pavlo Plynko
Pavlo Plynko
Java Developer at CodeGym

Java command line arguments

Published in the Java Developer group

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){…}


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)


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.


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


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. Java command line arguments - 3


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!