Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Beginners Command Line Arguments in Java Program

 
 

Share on Google+Share on Google+

Command Line Arguments in Java Program

Advertisement
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

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: June 27, 2008

Related Tutorials

Discuss: Command Line Arguments in Java Program   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:14
debendra kindo
February 25, 2012
definition of command line argument

Course subjectWeb tech.-II (Core Java) PLEASE SEND THE PERFECT DEFINITION IN MY MAIL-ID
jasmin
March 1, 2012
CSE

Great
Anto
April 28, 2012
cant we reference the supplied args directly

Good tut. i guess u can use this a simpler example. :) String var1 = args[0]; System.out.print(var1);
nishakantsharma
May 17, 2012
java

java is a product of sun muicrosystems
prakash binwal
May 24, 2012
java

command line argument do not understand.
Basileen
May 24, 2012
Thanx

Thanks a lot... Really helped me very much :)
Mohammed Aamir Shareef
June 28, 2012
Thanks

This was so helpful. thank you so much... :)
pooja
August 2, 2012
phrase-o-matic

i want to know about phrase-o-matric....if u know than pls suggest....
Dishant h shah
September 11, 2012
java

pls help me..
DMCA.com