How to pass Command-line Arguments in Eclipse IDE?

18 January 2012 By Nithya Vasudevan 18,092 views 5 Comments
3 Flares Twitter 1 Facebook 2 Google+ 0 3 Flares ×

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

Tags:

  • sai

    excellent tutorial for Beginners..

  • aanaidu

    This is very nice article. Thank You so much.

  • Georges

    This the best explanation i’ve found on the web.
    Thank you for your effort

  • Anu

    Thank you so much ! This was the best explanation for my question.

  • random

    many thanks.