Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read SiteMap

Tags: , , , ,

Sponsored Links



What are Command-Line Arguments in Java?

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 20-02-2010
Beter 2 Burn Out's Avatar
Member
 
Join Date: Aug 2006
Posts: 136
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.
Reply With Quote
  #2  
Old 20-02-2010
Allan.d's Avatar
Member
 
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.
Reply With Quote
  #3  
Old 20-02-2010
Warner's Avatar
Member
 
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.
Reply With Quote
  #4  
Old 20-02-2010
Praetor's Avatar
Member
 
Join Date: Apr 2008
Posts: 1,937
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]);
        }
    }
}
Reply With Quote
  #5  
Old 20-02-2010
kelfro's Avatar
Member
 
Join Date: Apr 2008
Posts: 1,976
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.
Reply With Quote
  #6  
Old 20-02-2010
opaper's Avatar
Member
 
Join Date: May 2008
Posts: 2,362
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);
        }
    }
}
__________________
The FIFA Manager 2009 PC Game
Reply With Quote
  #7  
Old 20-02-2010
Reegan's Avatar
Member
 
Join Date: Oct 2005
Posts: 2,299
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);
    }
}
Reply With Quote
  #8  
Old 20-02-2010
Member
 
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
Reply With Quote
  #9  
Old 20-02-2010
Beter 2 Burn Out's Avatar
Member
 
Join Date: Aug 2006
Posts: 136
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.
Reply With Quote
Reply

  TechArena Community > Software > Software Development


Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "What are Command-Line Arguments in Java?"
Thread Thread Starter Forum Replies Last Post
Windows Phone 7 Error: Connection failed because of invalid command-line arguments Saqlain Portable Devices 3 28-10-2010 01:16 PM
Command Line Arguments in java Vipul03 Software Development 2 22-02-2010 04:39 PM
I/O from the Command Line in Java Damien25 Software Development 5 19-02-2010 12:22 AM
PHP command line arguments Sori Software Development 3 24-09-2009 01:45 PM
How to get command line arguments for windows application in c# Aaryn Software Development 2 08-07-2009 01:11 AM


All times are GMT +5.5. The time now is 05:26 AM.