Report this

What is the reason for this report?

Command Line Arguments in Java

Published on August 3, 2022
Command Line Arguments in Java

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 our products

About the author

Pankaj Kumar
Pankaj Kumar
Author
See author profile

Java and Python Developer for 20+ years, Open Source Enthusiast, Founder of https://www.askpython.com/, https://www.linuxfordevices.com/, and JournalDev.com (acquired by DigitalOcean). Passionate about writing technical articles and sharing knowledge with others. Love Java, Python, Unix and related technologies. Follow my X @PankajWebDev

Category:
Tags:
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.

Still looking for an answer?

Was this helpful?

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

- Zanik

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

- Vishu

Creative CommonsThis work is licensed under a Creative Commons Attribution-NonCommercial- ShareAlike 4.0 International License.
Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.