Home Java Beginners Command Line Arguments in Java Program



Command Line Arguments in Java Program
Posted on: June 27, 2008 at 12:00 AM
Java application can accept any number of arguments directly from the command line.

Command Line Arguments in Java Program

     

Java application can accept any number of arguments directly from the command line. The user can enter command-line arguments when invoking the application. When running the java program from java command, the arguments are provided after the name of the class separated by space. For example, suppose a program named CmndLineArguments that accept command line arguments as a string array and echo them on standard output device.

java CmndLineArguments Mahendra zero one two three

CmndLineArguments.java

   /**
   *
How to use command line arguments in java program.
   */
   class CmndLineArguments {

  public static void main(String[] args) {
    int length = args.length;
    if (length <= 0) {
    System.out.println("You need to enter some arguments.");
    }
   for (int i = 0; i < length; i++) {
    System.out.println(args[i]);
   }
   }
   }

Output of the program:

Run program with some command line arguments like: 
java CmndLineArguments Mahendra zero one two three

OUTPUT
Command line arguments were passed :
Mahendra
zero
one
two
three

Download source code

Related Tags for Command Line Arguments in Java Program:
javacstringcomidecommand-linearrayclassapplicationiouseroutputargumentscommandvinumbernamelineidappechodeviceforexampleprogramstandardramexamposrunargumentearespacecommacanlinearliputusemandevfromceenterinasstamntoutpartrseparateoscaosjndaafteraceclrunningemdirnamedmepropprateacceptcatctlxawhenxampsrectsuspdirectatanypackirhaicemplrayeaandarccstrvassriringrdthcmavstatiapafhatinvokingicaicapleplprndonomogro


Ask Questions?    Discuss: Command Line Arguments in Java Program   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.