Java Command Line Arguments

What is a command line argument? Good question and glad you asked. A Java application can accept any number of arguments or inputs from the command line. These inputs are information or details on how your Java program should run.

When you run a Java program, the Java Runtime will pass all arguments on the command line to the main method of the Java application in the form of an array of Strings.


Passing Command Line Arguments

The process to pass in arguments to your Java program is simple. From the command line, you include the variables after your program name. In this example, my program name is HelloWorld and I am passing in 3 arguments “Java” “Code” “Depot”.


Passing Command Line Arguments From Eclipse

If your like me, you do all of your Java programming in Eclipse. Passing in arguments using Eclipse is just as simple. Click on Run » Run Configurations » Arguments and type in your arguments separated by spaces.


Reading Command Line Arguments

Any argument that is passed via the command line will end up in the String[] args, which is just an array of Strings. Since Java indexes start at 0, the first argument will be args[0] and then args[1] and so on.

//Store first argument
String s1 = args[0];
System.out.println("\nFirst Argument: "+s1);
			
//Store second argument
String s2 = args[1];
System.out.println("\nSecond Argument: "+s2);
			
//Store third argument
String s3 = args[2];
System.out.println("\nThird Argument: "+s3);


Java Command Line Arguments Source Code

Here is the full Java source code that will first check to make sure the number of arguments passed equals three and then go ahead and store each argument in a String.

package com.jcd.java.tutorials;

public class JavaCommandLineArgs 
{

	public static void main(String[] args) 
	{
		
		//Print out the number of agruments passed
		System.out.println("Number of Command Line Arguments: "+args.length);
		
		//Method to process command line arguments
		processCommandLineArgs(args);
		
	}
	
	public static void processCommandLineArgs(String[] args)
	{
		
		//Check to make sure we got exactly 3 command line arguments
		if(args.length == 3)
		{
			//Store first argument
			String s1 = args[0];
			System.out.println("\nFirst Argument: "+s1);
			
			//Store second argument
			String s2 = args[1];
			System.out.println("\nSecond Argument: "+s2);
			
			//Store third argument
			String s3 = args[2];
			System.out.println("\nThird Argument: "+s3);
		}
		else 
		{
			System.out.println("Was expecting 3 arguments but received "+args.length);
		}
		
	}

}
Number of Command Line Arguments: 3

First Argument: Java

Second Argument: Code

Third Argument: Depot



Concluding Comments

Remember that all command line arguments are of type String. If you want to pass a number like 100, it will be passed as a String, but you would need to convert it to an integer when you store it. For example, if our first argument needs to be an Integer, use the Integer.parseInt() method to convert the String to an int.

//Store first argument as an Integer
in i1 = Integer.parseInt(args[0]);



Java References:

Add a Comment

Your email address will not be published. Required fields are marked *