The concept that we are going to talk today is about printing i.e. accessing the elements or arguments that we enter on the command line in the runtime.

There are plenty of ways to implement this thing. For naïve programmers, we can use the simple editor and then run the program on the command line.

For those users who use eclipse IDE for java development, it can be done in two different ways.

The first and the conventional way is to create a scanner object and then accept the arguments from the command line and then print those arguments using a simple while loop.

The second way is to configure those command line arguments using the RUN CONFIGURATION feature under the RUN AS category when you right click on the program to run.


All these approaches are explained in detail in the following way. Please have a closer look.


In case of command line,

The sample program is written on a text editor like gedit in Unix/Linux systems, notepad or notepad++ in windows or anything of this kind. Once you are done with the program, you save it with .java extension and then run the code directly in the TERMINAL in Unix/Linux based systems or use command line for doing the same on windows machines.

In my case, I am using the command line client to run this program and the code is written like this.

public class PrintCommandLineArguments{public static void main(String [] args){

for(int I = 0 ; i< args.length ; i++)





Save above program as and then copy this to the Unix/Linux based system in the desired location.

Compile this .java file using JAVAC command, then a .class file will get created with the name PrintCommandLineArguments.class, you need to run this file using JAVA command. One thing you need to keep in mind that while running this .class file, pass all the command line arguments that you want your program to handle.

For example, suppose you want to pass arguments “I am a good boy”, then you need to run the .class file as follows

java PrintCommandLineArguments I am a good boy

This will run the program using I am a good boy as the arguments.

The screenshots shared below will clear all the doubts. Please have a close look.


command line arguments
command line arguments






It is a simple approach. In this, we create a scanner object which is imported from the package java.util.Scanner. All the command line arguments are accessed with the help of this scanner object only.

The code written below is self-explanatory.

package samplejavaprograms; 

import java.util.Scanner;


publicclass PrintCommandLineArguments {

publicstaticvoid main(String[] args) {


Scanner scanner = new Scanner(;

String arg = “”, temp = “”;

  1. out.println(“enter command line arguments”);


while (!(arg.equalsIgnoreCase(“q”))) {

arg =;

temp = temp + ” ” + arg;


temp = temp.trim();

  1. out.println(temp.substring(0, temp.length() – 1));






There are two important things to notice over here.

The first one is the while loop which we are using. In this while loop, we are checking for the “q” character. As long as we don’t enter “q”, it will accept all the command line arguments, but once we enter “q”, it will come out of the while loop since the condition for the while loop becomes false.

And the second thing is that in order to avoid the extra spaces coming into final string, we have used trim() method. This method ensures that all the extra spaces will be removed in order to create a stable string.

And one more thing, if you instantiate your strings arg and temp equal to null, then it will throw a null pointer exception, and that is why you need to make it equal to an empty character. This way you can avoid the null pointer exception that you face with null.

At last, after printing the resulted string, you need to close the scanner object otherwise it shows the compile time error.

If you take a close look at the output, you will see that we have used a substring method in order to retrieve the correct run time arguments. We are taking the substring from the start index to last but one index i.e. neglecting “q” character. We got the correct output using the above program and it is shown correctly in the output marked with red marker.



Print Command Line Arguments using Scanner object in eclipse IDE





This method is also known as the hidden method of using the run time arguments. In this method, you need to configure the run time arguments you want to pass in advance while executing the java program. This run configurations option is available under RUN AS option which becomes visible once you right click on the java program.

A new screen comes up once you select the RUN CONFIGURATIONS option. In that screen, you need to select the ARGUMENTS tab and specify all the run time arguments that you want to pass while executing the java program in the picture. The entire process is explained in the screenshots shared below along with the code.

Have a good look at the flow of execution of the program.

As you can see clearly, we select Run Configurations under Run As menu which becomes visible once you right click on the desired java program.


Run Configurations option under Run As menu


After that you select the Arguments tab and enter all the arguments possible. Once you enter all the arguments, you click on Apply and then on Run. Once you click Run, the program will execute with those arguments that you specified in the Arguments tab.


Arguments Tab Window


Taking a careful look at the output, you will see that all the run time arguments get printed on the output console and you did it with only two lines of code. So this seems quite feasible but the main problem with this approach is you need to specify the arguments in advance which might create problems some time.


Output Console of Run Configurations type


So according to me second option is the best one to choose from three since it gives you the liberty to add whatever you want at the run time and that too in a neat manner.


Hope you all had a great read. Thank you. Suggestions are always welcome.

Published by milindjagre

I founded my blog four years ago and am currently working as a Data Scientist Analyst at the Ford Motor Company. I graduated from the University of Connecticut pursuing Master of Science in Business Analytics and Project Management. I am working hard and learning a lot of new things in the field of Data Science. I am a strong believer of constant and directional efforts keeping the teamwork at the highest priority. Please reach out to me at for further information. Cheers!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

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

%d bloggers like this: