Tuesday, December 4, 2012

How to pass arguments to "main" method in Eclipse or MyEclipse?

If you are running your main class (having main method) in console, for passing command line arguments, you could have done the following way:
java HelloWorld arg1 arg2
But many beginners find it difficult to pass as many arguments they wish if they are working on an IDE like Eclipse. So here are some simple steps to configure permanently for specific programs so that whenever they are run, automatically the programmer is asked for arguments.
Pass as many number of arguments you want, even zero.
1. Right click your program (package or .java) → PropertiesRun/Debug Settings
2. Then click on New button on right side, select Java Application and click on Ok button.
3. Since you set it up as a Java application, it ought to have a main class (a class having the main method). Mention the fully qualified class' name in Main tab.
4. Click on (x)= Arguments tab, and in Program Arguments section, write the following:
${string_prompt:<your message>}
For example,
${string_prompt:this program's arguments:}
4. Click on Ok and again Ok button to save the setting. Thats it.

Now whenever you run the program, run it as a Java Application, by clicking on
Run As → Java Application, and you are having it!

Note: Advanced users may do a similar addition VM Arguments section for passing arguments for addressing heap size and others to the JVM.

No comments:

Post a Comment

Liked or hated the post? Leave your words of wisdom! Thank you :)