How to Pass Arguments in JAVA main method in eclipse

Argument in JAVA main

 218 total views

In this article, we will learn about How to pass an argument in JAVA main method while working with eclipse IDE.

In the previous eclipse tutorial blog, I have discussed about JAVA call stack analysis and today we will discuss about eclipse run configurations.

As a JAVA beginner, the first program that we usually make is the “Hello-World” mostly using eclipse.

This is our first encounter with the JAVA main method.

But If you want to deep dive in the main method’s signature then the few questions you ask are

What is String args[]?

What is the use of String args[]?

How can I pass this input array in eclipse?

So String args[] is an array of string that can be used to get the JAVA command line argument for the main method.

String args[] are there to provide your application some flexibility and it will help your app to behave dynamically as per the user input.

Now a developer will say I am working on eclipse IDE and running my programs by clicking on the run button.

So do I need to run my program from the command line to explore String args[]?

The answer is. No.

We can explore String args[] functionality from eclipse as well.

An Argument in JAVA main method

The first step will be to create a new entry in the Run Configuration.

It can be found under Run menu Run => Run Configuration.

Run Configuration

On Clicking the Run Configuration, a Pop-up window will open.

Select the JAVA application and right-click to add a new configuration.

run configuration pop up
Run Configuration

Mostly your JAVA project is selected when you “New Configuration” section opens but in case your project is not selected then you can browse and select your project and click Ok.

project selection
Project Selection

Here I am selecting the Math project for this demo.

After selecting your project, you have to select your main Method’s Class and click Ok.

select main class
Select your main method class

Now provide the Arguments for your main method.

In this demo, I will take a few numbers as input and will print their sum on the console.

The Argument tab is just adjacent to the Main tab.

Argument in JAVA main

After providing the Arguments, provide a name to the configuration like I have named the above configuration as “Sum Demo”.

Now click Run directly or you can apply the changes and run it later.

I am running it directly.

Let’s see the output generated:

The Sum is: 15

The output will remain the same as we have fixed the arguments.

Getting dynamic arguments

Now there exists a way to get the dynamic argument from an input window.

Let’s explore this option.

To accept dynamic input we have to make a few changes in the argument tab of Run configuration.

dynamic run configuration
Eclipse Run Configuration for Dynamic input

You need to define ${string_prompt} in the Program arguments section, now eclipse will ask for your input dynamically.

input pop-up

Output

The Sum is: 15

Note: “string_prompt” is fixed and you must not provide any other name, If you do provide then eclipse will prompt an error.

error pop-up
Problem Occured

Always remember the single input window will take as many arguments as you want to pass separated by a space.

In conclusion, I will say this eclipse feature enables us to run programs that require JAVA command-line argument as input and can be used to create applications and executables JARs that require dynamic input by passing an argument in JAVA main method.

Leave a Reply

Your email address will not be published. Required fields are marked *