Header javaperspective.com
JavaPerspective.com  >   Beginner Tutorials  >   5. Java in Practice  >   5.1. Command-line arguments

5.1. Command-line arguments
Last updated: 27 January 2013.

Command-line arguments are values that are passed in to an application at startup through the command prompt (for Windows users) or terminal (for Linux and Mac users).

The following class is a simplified version of the class Converter that I have used in the tutorial Exceptions. It contains a single method that converts a string into an integer:

public final class Converter {

   
public int stringToInt(String value){

         
return Integer.parseInt(value);

   
}

}

I also used the class App shown below to call the method stringToInt:

public final class App {

   
public static void main(String[] args) {

         
int myInteger = 0;

         
try{
               
myInteger = new Converter().stringToInt("123");
                System.out.println
(myInteger);
         
}
         
catch(NumberFormatException e){
               
System.out.println("A NumberFormatException has been thrown by the method stringToInt()");
         
}
    }

}

If you want to change the argument that is passed in to the method stringToInt, you have to modify and recompile the class App before executing it with the following command:

java App

Command-line arguments provide more flexibility for applications by allowing users to pass arguments to the main method after the command that starts the application. For example, I can pass the command-line argument 123 to the main method by typing the following command:

java App 123

As a result, at runtime, the string value "123" will be contained in the main method's array of strings argument (String[] args).

Let's modify the class App accordingly:

public final class App {

   
public static void main(String[] args) {

         
int myInteger = 0;

         
try{
               
myInteger = new Converter().stringToInt(args[0]);
                System.out.println
(myInteger);
         
}
         
catch(NumberFormatException e){
               
System.out.println("A NumberFormatException has been thrown by the method stringToInt()");
         
}
    }

}

In the example shown above, the array args contains a single element which is the string value "123". However, you can pass as many command-line arguments as you want, using the space character as a separator. For example, you can run the class App as follows:

java App 123 54 -2 78 33

Since I passed several command-line arguments to the main method, I can modify it in order to call the method stringToInt in a loop for each command-line argument as shown below:

public final class App {

   
public static void main(String[] args) {

         
int myInteger = 0;

         
try{
               
if(args.length > 0){
                     
Converter converter = new Converter();

                     
for(String s : args){
                           
myInteger = converter.stringToInt(s);
                            System.out.println
(myInteger);
                     
}
                }
               
else{
                     
System.out.println("No command-line arguments");
               
}
          }
         
catch(NumberFormatException e){
               
System.out.println("A NumberFormatException has been thrown by the method stringToInt()");
         
}
    }

}

The output is:

123
54
-2
78
33

As you can see, I created an instance of the class Converter outside the loop because creating objects in Java consumes time and memory. Consequently, you should avoid creating unnecessary objects, especially if performance is important for your application, which is usually the case.


Code exercise

Rewrite the class App to make it simply print the command-line arguments to the standard output without calling the method stringToInt. If you run the class App with the command shown below:

java App Africa America Antarctica Asia Europe

the output must be the following:

Africa
America
Antarctica
Asia
Europe


public final class App {

   
public static void main(String[] args) {

         
for(String s : args)
               
System.out.println(s);
   
}
}

The loop can be written in a different way as shown below:

for(int i=0; i<args.length; ++i)
   
System.out.println(args[i]);


Note that if a single command-line argument is composed of several words, they must be enclosed in quotation marks:

java App Africa "America Antarctica" Asia Europe

"America Antarctica" is considered as a single argument. As a result, the output for that command is:

Africa
America Antarctica
Asia
Europe


You are here :  JavaPerspective.com  >   Beginner Tutorials  >   5. Java in Practice  >   5.1. Command-line arguments
Next tutorial :  JavaPerspective.com  >   Beginner Tutorials  >   5. Java in Practice  >   5.2. Reading user input

Copyright © 2013 - 2016. JavaPerspective.com. All rights reserved.  ( Terms | Contact | About ) 
Java is a trademark of Oracle Corporation
Image 1 Image 2 Image 3 Image 4 Image 5 Image 6 Image 7