A Java application can accept any number of arguments from the command line. This allows the user to specify configuration information when the application is launched.The user enters command-line arguments when invoking the application and specifies them after the name of the class to be run. For example, suppose a Java application called
Sortsorts lines in a file. To sort the data in a file namedfriends.txt, a user would enter:When an application is launched, the runtime system passes the command-line arguments to the application's main method via an array ofjava Sort friends.txtStrings. In the previous example, the command-line arguments passed to theSortapplication in an array that contains a singleString:"friends.txt".
Theexample displays each of its command-line arguments on a line by itself:EchoThe following example shows how a user might run/* * Copyright (c) 1995 - 2008 Sun Microsystems, Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * - Neither the name of Sun Microsystems nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ public class Echo { public static void main (String[] args) { for (String s: args) { System.out.println(s); } } }Echo. User input is in italics.Note that the application displays each word —java Echo Drink Hot Java Drink Hot JavaDrink,Hot, andJava— on a line by itself. This is because the space character separates command-line arguments. To haveDrink,Hot, andJavainterpreted as a single argument, the user would join them by enclosing them within quotation marks.java Echo "Drink Hot Java" Drink Hot Java
If an application needs to support a numeric command-line argument, it must convert aStringargument that represents a number, such as "34", to a numeric value. Here is a code snippet that converts a command-line argument to anint:int firstArg; if (args.length > 0) { try { firstArg = Integer.parseInt(args[0]); } catch (NumberFormatException e) { System.err.println("Argument must be an integer"); System.exit(1); } }parseIntthrows aNumberFormatExceptionif the format ofargs[0]isn't valid. All of theNumberclasses —Integer,Float,Double, and so on — haveparseXXXmethods that convert aStringrepresenting a number to an object of their type.