Command-line Arguments

Sang Shin, sang.shin@sun.com, Sun Microsystems, www.javapassion.com/javaintro


In this hands-on lab, you will learn how to pass command line arguments to a Java program both at the command line

Expected duration: 30 minutes

Prerequisites

This hands-on lab assumes you have no or minimum programming experience.

Software Needed

Before you begin, you need to install the following software on your computer. 


Change Log



Lab Exercises


Exercise 1: Read command-line arguments


(1.1) Build and run Hello Java program that receives command-line arguments

1. Go to a directory where you want to create Java program

C:\>cd \myjavaprograms

2. Write HelloCommandLineArguments.java using your editor of choice such as notepad on Windows platform or gedit on Solaris platform (in this example, I am using jedit) as shown in Code-1.10 below. 

C:\myjavaprograms>jedit HelloCommandLineArguments.java

public class HelloCommandLineArguments {
   
    public static void main( String[] args ){
       
        // Print the string "Hello, " on screen
        System.out.println("I am saying Hello to the people below.. ");

        // Check if a command line argument exists
        if(args.length == 0)
            System.exit(0);
           
        // Display the arguments from the command line
        for(int counter = 0; counter < args.length; counter++){
            System.out.println("argument index " + counter + ": " + args[counter]);  
        }
    }

}
Code-1.10: HelloCommandLineArguments.java

3. Compile HelloCommandLineArguments.java using javac compiler. 

C:\myjavaprograms>javac HelloCommandLineArguments.java

4. Run the HelloCommandLineArguments program using java command passing arguments.


C:\myjavaprograms>java HelloCommandLineArguments David Charles Young
I am saying Hello to the people below..
argument index 0: David
argument index 1: Charles
argument index 2: Young

                                                                                                                        return to top of the exercise

(1.2) Read numbers as arguments

1. Go to a directory where you want to write Java source files.

C:\>cd \myjavaprograms

2. Write MyCompute.java using your editor of choice such as notepad on Windows platform or gedit on Solaris platform (in this example, I am using jedit) as shown in Code-1.20 below. 

C:\myjavaprograms>jedit MyCompute.java

public class MyCompute {
   
    public static void main(String[] args) {
        System.out.println("I am reading numbers as command line arguments.. ");
       
        // Check if a command line argument exists
        if(args.length != 2){
            System.out.println("Please enter two numbers!");
            System.exit(0);
        }
       
        // Display the addition of the two numbers
        int int1 = Integer.parseInt(args[0]);
        int int2 = Integer.parseInt(args[1]);
        int additionResult = int1 + int2;
        System.out.println("Result of addition = " + additionResult);
       
        // Display the multiplication of the two numbers
        int1 = Integer.parseInt(args[0]);
        int2 = Integer.parseInt(args[1]);
        int multiResult = int1 * int2;
        System.out.println("Result of multiplication = " + multiResult);
    }
   
}
Code-1.20: MyCompute.java

3. Compile MyCompute.java using javac compiler. 

C:\myjavaprograms>javac MyCompute.java

4. Run the MyCompute program using java command passing two number arguments.

C:\myjavaprograms>java MyCompute
I am reading numbers as command line arguments..
Please enter two numbers!

C:\myjavaprograms>java MyCompute 4
I am reading numbers as command line arguments..
Please enter two numbers!

C:\myjavaprograms>java MyCompute 4 6
I am reading numbers as command line arguments..
Result of addition = 10
Result of addition = 24

                                                                                                                                             Return to top of the exercise

Summary

In this exercise, you learned how to read input arguments in your Java application.
   
                                                                                                                                   Return to the top


Exercise 2: Pass command-line arguments using NetBeans

In this exercise, you are going to learn how to pass arguments when you are using NetBeans.  The programs you built above are already provided as NetBeans projects.

  1. Open, build and run HelloCommandLineArguments NetBeans project
  2. Open, build and run MyCompute NetBeans project

(2.1) Open, build and run HelloCommandLineArguments NetBeans project

1.  Open HelloCommandLineArugments NetBeans project. 


Figure-2.10: pen HelloCommandLineArguments project
2. Pass command-line arguments


Figure-2.11: Open Properties of the project


Figure-2.12: Provide command line arguments

3. Build and run the project.

Figure-2.13: Result

                                                                                                                        return to top of the exercise


(2.2) Open, build and run MyCompute NetBeans project

1.  Open MyCompute NetBeans project. 

2. Pass command-line arguments

Figure-2.21: Pass command line arguments

3. Build and run the project.

Figure-2.13: Result

                                                                                                                       return to top of the exercise

Summary

In this exercise,  you learned how to pass command-line arguments when you are using NetBeans.

                                                                                                                        return to the top




Homework exercise (for people who are taking Sang Shin's "Java Programming online course")


1. The homework is to create a new NetBeans project called "MyOwnCommandLineArguments" as following:
2. Send the following files to javaintro1homework@sun.com with Subject as JavaIntro-commandarguments.