How to use command line arguments in IDEs?

Posted by
I know its not so tough to pass the command line arguments still we usually forget it. Because we do not use it frequently. And when the question comes to pass the argument in any IDE then it really makes us in trouble. Dont know in your case but I faced it in my early career when in a machine test I was supposed to write a program of command line argument in NetBeans. So I am sharing it. May be it can help you.

Command Line Argument

As we know there is a method in java “public static void main(String[] args)”.  Its a method for which JVM search to run a program. Without this method you can compile your program but can not execute it. We use args as an argument for main method, args is a string array. Its just an argument so you can use any variable name in place of “args”(Just a variable name). Now come to the point what is command line argument. Command line arguments are values which are passed to main method in String array at run time. So how we use command line arguments in our program?  Its indexing starts from 0. The first value which we will pass at run time, will store in args[0]. We will discuss about three places to use command line argument.

1. Command Prompt


  * @author Java2Career  
 public class CommandLineArgument {  
   public static void main(String[] args) {  
     String stringArugument=args[0];  
   //will give an error because args data type is String so will have to convert it  
   //int integerArgument=args[1];  
     int integerArgument=Integer.parseInt(args[1]);  
     float floatArgument=Float.parseFloat(args[2]);  
     System.out.println("String command line argument: "+stringArugument);  
     System.out.println("Integer command line argument: "+integerArgument);  
     System.out.println("Float command line argument: "+floatArgument);  
    System.out.println("no of command line argument "+args.length);  


args is a variable of String array type so taking input of any non string variable you will have to convert args into that type. For ex. To convert in int we used Integer.parseInt(args[1]). We can also know the number ovf passed command line argument by args.length.



2. NetBeans

Create a new project in NetBeans. Copy the above code in your main class. Follow the following steps:
  1. Go to project and right click on project that you have created.
  2. Now click on “Properties”.
  3. It will pop up the new window “Project Propeties”.
  4. In categories click on “Run”.
  5. Pass the values in arguments text box like following image.
  6. Click on “Ok”.
  7. Then press f6 to run or in menu bar go to Run->Run Main Project.



Create a new project in Eclipse. Copy the above code in your main class. Follow the following steps:

  1.  Go to project and right click on it then go to “Run As-> Run Configuration”.
  2.  It will pop up window “Run Configartion” as shown in image.
  3. Click on “(x)=Arguments” tab.
  4. Click on “Run”.




    Leave a Reply