Java Programming, Lecture Notes # 32, Revised 10/03/99.
Students in Prof. Baldwin's Introductory Java Programming classes at ACC are responsible for knowing and understanding all of the material in this lesson (except that they are not responsible for detailed information that is specific to C++).
The detailed material on C++ is provided as supplementary material for the benefit of those persons who are already familiar with C++ and who are making the transition into Java.
Familiar example from DOS
In both Java and C++, the program can be written to accept command-line-arguments.
DOS users will be familiar with commands such as the following:
copy fileA fileB |
In this case, copy is the name of the program to be executed, and fileA and fileB are command-line arguments.
C++ syntax for command-line arguments
Java and C++ handle command-line arguments in a different manner. C++ programmers will be familiar with the following syntax which is used in the main() function to capture command-line arguments. |
void main(int argc, char *argv[]){ . . . }//end main function |
In this case, the operating system passes in two parameters to the function. argc is an integer specifying the number of command-line arguments, while argv is an array of pointers where each pointer in the array points to one of the command-line arguments stored as a string somewhere in memory. In some systems, argv[0] points to a string that is the name of the program. In other systems, argv[0] points to garbage. In C++, if the program is not written to accept command-line arguments, it is not necessary to specify the parameters in the argument list of the function signature. |
Java syntax for command-line arguments
The Java syntax for command-line arguments is as follows. Note that in Java, the parameters to main() must appear in the method signature whether or not the program is written to support the actual use of command-line arguments.
public static void main(String[] args){ . . . }//end main method |
Where are the arguments stored?
In this case, each of the elements in the array named args (including the element at position zero) is a reference to one of the command-line arguments each of which is a String object.
What about the name of the program?
Unlike C++, the name of the program is not provided in Java. |
How many arguments did the user enter?
Recall from an earlier lesson on arrays that the number of elements in a Java array can be obtained from the length property of the array. Therefore, it is not necessary for the operating system to also pass a parameter specifying the number of arguments.
Sample Java program
The use of command-line arguments is illustrated in the following example Java program. The output from running this program for a specific input is shown in the comments at the beginning of the program.
/*File cmdlin01.java Copyright 1997, R.G.Baldwin This Java application illustrates the use of Java command-line arguments. When this program is run from the command line as follows: java cmdlin01 My command line arguments the program produces the following output: My command line arguments ********************************************************/ class cmdlin01 { //define the controlling class public static void main(String[] args){ //main method for(int i=0; i < args.length; i++) System.out.println( args[i] ); }//end main }//End cmdlin01 class. |
Sample C++ program
For comparison purposes, the following C++ program is structured to emulate the behavior of the Java program. Again, the output from running the program with a specific command-line input is shown at the beginning of the program. Note that the system used to compile and run this program did display the name of the program in the output. |
/*File cmdlin01.cpp Copyright 1997, R.G.Baldwin This C++ application illustrates the use of command-line arguments. When this program is run with the following input c:\jnk\cmdlin01 My command line arguments it produces the following output C:\JNK\CMDLIN01.EXE My command line arguments ********************************************************/ #include<iostream.h class cmdlin01 { public: static void classMain(int argc, char* argv[]); };//End cmdlin01 class definition. //The loop in this function requires the function to be // defined outside the class (loops are not allowed in // inline functions) void cmdlin01::classMain(int argc, char* argv[]){ for(int i = 0; i < argc; i++) cout << argv[i] << endl; }//end classMain //=====================================================// void main(int argc, char* argv[]) { //call the class method named classMain cmdlin01::classMain(argc,argv); }//end main |
Q - Provide a common example of a command-line statement that illustrates the use of command-line-arguments.
A - Java programs can be written to accept command-line-arguments. DOS users will be familiar with commands such as the following:
copy fileA fileB |
In this case, copy is the name of the program to be executed, and fileA and fileB are command-line arguments.
Q - Describe the purpose of command-line-arguments.
A - Command-line-arguments are used in many programming and computing environments to provide information to the program at startup that it will need to fulfill its mission during that particular invocation.
Q - In Java, syntax provisions must be made in the method signature for the main method to accommodate command-line-arguments even if the remainder of the program is not designed to make use of them: True or False. If False, explain why.
A - True. In Java, syntax provisions must be made in the method signature for the main method to accommodate command-line-arguments even if the remainder of the program is not designed to make use of them.
Q - Provide the method signature for the main method in a Java application that is designed to accommodate the use of command-line-arguments. Identify the part of the method signature that applies to command-line-arguments and explain how it works.
A - The Java syntax for command-line arguments is as follows.
public static void main(String[] args){ . . . }//end main method |
In this case, each of the elements in the array named args (including the element at position zero) is a reference to one of the command-line arguments each of which is a String object.
Q - Explain how a Java application can determine the number of command-line-arguments actually entered by the user.
A - The number of elements in the array of references to the String objects that contain the command-line-arguments can be obtained from the length property of the array.
Q - Write a Java program that meets the following specifications.
/*File SampProg11.java from lesson 32 Copyright 1997, R.G.Baldwin Without reviewing the following solution, write a Java application that illustrates the handling of command-line arguments in Java. Provide a termination message that displays your name. **********************************************************/ class SampProg11 { //define the controlling class public static void main(String[] args){ //define main for(int i=0; i < args.length; i++) System.out.println( args[i] ); System.out.println("Terminating, Dick Baldwin"); }//end main }//End SampProg11 class. |
-end-