Tutorial

Command Line Arguments in Java

Published on August 3, 2022
Default avatar

By Pankaj

Command Line Arguments in Java

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Command-line arguments in Java are used to pass arguments to the main program. If you look at the Java main method syntax, it accepts String array as an argument. When we pass command-line arguments, they are treated as strings and passed to the main function in the string array argument. The arguments have to be passed as space-separated values. We can pass strings and primitive data types as command-line arguments. The arguments will be converted to strings and passed into the main method string array argument.

Command Line Arguments in Java

Let’s say we have a simple java class to print command line arguments values.

package com.journaldev.examples;

public class CommandLineArguments {

	public static void main(String[] args) {
		System.out.println("Number of Command Line Argument = "+args.length);
		
		for(int i = 0; i< args.length; i++) {
			System.out.println(String.format("Command Line Argument %d is %s", i, args[i]));
		}
	}

}

If we run this class without any arguments, the output will be as follows.

$ java com/journaldev/examples/CommandLineArguments.java
Number of Command Line Argument = 0

Now, let’s pass some arguments to the main class. We have to pass the arguments as space-separated values.

$ java com/journaldev/examples/CommandLineArguments.java "A" "B" "C"
Number of Command Line Argument = 3
Command Line Argument 0 is A
Command Line Argument 1 is B
Command Line Argument 2 is C
$ java com/journaldev/examples/CommandLineArguments.java 1 2 3      
Number of Command Line Argument = 3
Command Line Argument 0 is 1
Command Line Argument 1 is 2
Command Line Argument 2 is 3
$

Note: If you are using Java 11 or higher, you don’t need to compile the java source file explicitly. The java command will compile and run the class simultaneously.

How to Pass Command Line Arguments in Eclipse

We can also pass command-line arguments to a program in Eclipse using Run Configurations.

Step 1: Open the Class Run Configurations Settings

From the class editor, right click and chose “Run As” -> “Run Configurations…”.

Eclipse Run Configurations
Eclipse Run Configurations

Step 2: Specify the Program Arguments in the Arguments Tab

In the pop up window, click on the Arguments tab. Then provide the command line arguments value in the “Program Arguments” text box.

Eclipse Command Line Arguments
Eclipse Command Line Arguments

Step 3: Click on the Run button

When you will click on the Run button, the run configurations will be saved and the program will execute with the specified command-line arguments.

Eclipse Command Line Arguments Example
Eclipse Command Line Arguments Example

If you run the class again, the saved run configuration will be used. So if you want to override the command-line arguments or remove them, you will have to open the run configurations window and make necessary changes.

Conclusion

The command-line arguments are used to provide values that are essential to run the program. For example, we can specify the database credentials to be used by the program. We can specify the configuration file location from where the program should pick the required values. Reference: Command-Line Arguments Oracle Docs

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about us


About the authors
Default avatar
Pankaj

author

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
June 26, 2020

I want the java program that takes 10 arguments as integer and find the maximum out of it

- Vishu

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    March 31, 2020

    Thank you! I love this post. That help me understand clearly!

    - Zanik

      Try DigitalOcean for free

      Click below to sign up and get $200 of credit to try our products over 60 days!

      Sign up

      Join the Tech Talk
      Success! Thank you! Please check your email for further details.

      Please complete your information!

      Get our biweekly newsletter

      Sign up for Infrastructure as a Newsletter.

      Hollie's Hub for Good

      Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

      Become a contributor

      Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

      Welcome to the developer cloud

      DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

      Learn more
      DigitalOcean Cloud Control Panel