How to pass Command-line Arguments in Eclipse IDE?

New Java Project

  • Create a new Java Project and name it as “CommandLineArguments

Right click on Package Explorer -> New -> Project -> Select Java Project

New package

  • Create a new Package and name it as “commandline

Right click on src -> New -> Package

New Class

  • Create a new Java Class and name it as “AdditionOfTwoNumbers” in the package “commandline“. Make sure you select main method option.

Right click on commandline package -> New -> Class

Paste this code in the main method.

package commandline;
public class AdditionOfTwoNumbers {
	public static void main(String[] args) {
		int a, b;
		a = Integer.parseInt(args[0]);
		b = Integer.parseInt(args[1]);
		
		int sum = a + b;
		System.out.println("The Sum is: " + sum);
	}
}

Save the program.

  • Press [Ctrl + s] to save your program

Run Configurations

Go to Run menu and select Run Configurations. Run Configurations dialogue box opens.

Run your program

  • Select your Java class from the left side column. If you do not find your class, then click on the “New launch Configuration” button, found above the class list.
  • In the right side menu select “Arguments” tab
  • In the “Program Arguments” text area type the input numbers separated by spaces or newline.
  • Click Run button.

Output

  • You will see the output in the console.

The Sum is: 32

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.