Results 1 to 9 of 9

Thread: What are Command-Line Arguments in Java?

  1. #1
    Join Date
    Aug 2006
    Posts
    221

    What are Command-Line Arguments in Java?

    Hi friends,
    I have recently started working with the Core Java. Now I am working with the basic classes that are used in Java. I want to know how command line arguments are passed in Java.? Since, you have solved my issues many times I thought that posting here would be helpful. Please tell me what are Command-Line Arguments in Java.?? Any other information would be grateful.!!
    AMD Sempron 2800+ @ 2Ghz
    Asus A7V8X-LA
    120Gb Seagate barracuda 7200Rpm Ultra-ATA 100
    Elixir 512mb DDR Pc3200 (Soon 1Gb)
    Club 3D Radeon 9600 256Mb
    Lite-On Cd & Dvd writer combo
    IDE-Dvd drive
    400w Psu
    Windows Xp Pro Sp2
    Advent Wireless Mouse & Keyboard.

  2. #2
    Join Date
    Mar 2008
    Posts
    672

    Re: What are Command-Line Arguments in Java?

    You can put any number of arguments in your Java application because, your Java application can accept any number of arguments from the command line. Command-line arguments allow the user to affect the operation of an application. It is not necessary for the operating system to also pass a parameter specifying the number of arguments. 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. You will come to know more about this while doing the coding.

  3. #3
    Join Date
    Mar 2008
    Posts
    349

    Re: What are Command-Line Arguments in Java?

    You will have to enter the command-line arguments when invoking the application. Lets take an example so that it would be useful for you to understand the concepts. Suppose a Java application called Align sorts lines in a file. To sort the data in a file named people.txt, a user would enter:
    Code:
    java Align people.txt
    When an application is launched, the runtime system passes the command-line arguments to the application's main method via an array of Strings.

  4. #4
    Join Date
    Apr 2008
    Posts
    1,948

    Re: What are Command-Line Arguments in Java?

    You are not having an idea of how to pass the command line arguments, so I thought that giving an demonstration would help you. The following example explains the same :
    Code:
    public class CmdArgsDemo{
        public static void main(String[] args){
            System.out.println("The following command line arguments were passed:");
            
            for (int i=0; i < args.length; i++){
                System.out.println("arg[" + i + "]: " + args[i]);
            }
        }
    }

  5. #5
    Join Date
    Apr 2008
    Posts
    2,005

    Re: What are Command-Line Arguments in Java?

    The user enters command-line arguments when invoking the application and specifies them after the name of the class to be run. The Java syntax for command-line arguments is as follows :
    Code:
    public static void main(String[] args){
    . . . 
    }
    Hope that this post would have helped you.

  6. #6
    Join Date
    May 2008
    Posts
    2,389

    Re: What are Command-Line Arguments in Java?

    The Echo example displays each of its command-line arguments on a line by itself :
    Code:
    public class ExampleEcho {
        public static void main (String[] args) {
            for (String s: args) {
                System.out.println(s);
            }
        }
    }

  7. #7
    Join Date
    Oct 2005
    Posts
    2,393

    Re: What are Command-Line Arguments in Java?

    If an application needs to support a numeric command-line argument, it must convert a String argument that represents a number. So i have provided you with a code snippet that converts a command-line argument to an int :
    Code:
    int oneArg;
    if (args.length > 0) {
        try {
            oneArg = Integer.parseInt(args[0]);
        } catch (NumberFormatException e) {
            System.err.println("Argument must be an integer");
            System.exit(1);
        }
    }

  8. #8
    Join Date
    Feb 2010
    Posts
    1

    Re: What are Command-Line Arguments in Java?

    This simple application displays each of its command line arguments on a line by itself:
    class Echo {
    public static void main (String args[]) {
    for (int i = 0; i < args.length; i++)
    System.out.println(args[i]);
    }

    So hopefully this command line useful in your programming feature.
    All the Best!!
    Sophie Johnson

  9. #9
    Join Date
    Aug 2006
    Posts
    221

    What are Command-Line Arguments in Java?

    Thank you for replying me and giving me explanation about what are Command-Line Arguments in Java and explanation it in details.

Similar Threads

  1. Replies: 3
    Last Post: 28-10-2010, 12:16 PM
  2. Command Line Arguments in java
    By Vipul03 in forum Software Development
    Replies: 2
    Last Post: 22-02-2010, 04:39 PM
  3. I/O from the Command Line in Java
    By Damien25 in forum Software Development
    Replies: 5
    Last Post: 19-02-2010, 12:22 AM
  4. PHP command line arguments
    By Sori in forum Software Development
    Replies: 3
    Last Post: 24-09-2009, 12:45 PM
  5. Replies: 2
    Last Post: 08-07-2009, 12:11 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,022,817.88641 seconds with 17 queries