일요일, 5월 19
Shadow

#007 Command-line arguments

 

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

(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 multiplication = 24

Summary

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

 

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.

  • Select File->Open Project (Ctrl+Shift+O). The Open Project dialog box appears.
  • Browse down to <LAB_UNZIPPED_DIRECTORY>/commandarguments/samples directory.
    • Windows: If you unzipped the hands-on lab zip file, 1038_commandarguments.zip, under C:\ directory, the directory to which you want to browse down should be C:\commandarguments\samples.
    • Solaris/Linux: If you unzipped the 1038_commandarguments.zip file under $HOME directory, the directory to which you want to browse down should be $HOME/commandarguments/samples.
  • Select HelloCommandLineArugments.
  • Click Open Project Folder.  (Figure-2.10 below)


Figure-2.10: pen HelloCommandLineArguments project

  • Observe that the HelloCommandLineArugments project node is displayed under Projects pane of the NetBeans IDE.

2. Pass command-line arguments

  • Right click HelloCommandLineArugments project node and select Properties.


Figure-2.11: Open Properties of the project

  • Observe that the Project Properties dialog box appears.
  • Select Run under Categories section on the left.
  • For the Arguments field on the right, enter command line arguments, for example, David Charles Monica, in this example.  (Figure-2.12 below)
  • Click OK.


Figure-2.12: Provide command line arguments

3. Build and run the project.

  • Right click HelloCommandLinekArguments project node and select Run Project.
  • Observe the result in the Output window of the IDE. (Figure-2.13 below)


Figure-2.13: Result

(2.2) Open, build and run MyCompute NetBeans project

1.  Open MyCompute NetBeans project.

  • Select File->Open Project (Ctrl+Shift+O). The Open Project dialog box appears.
  • Browse down to <LAB_UNZIPPED_DIRECTORY>/commandarguments/samples directory.
    • Windows: If you unzipped the hands-on lab zip file, 1038_commandarguments.zip, under C:\ directory, the directory to which you want to browse down should be C:\commandarguments\samples.
    • Solaris/Linux: If you unzipped the 1038_commandarguments.zip file under $HOME directory, the directory to which you want to browse down should be $HOME/commandarguments/samples.
  • Select MyCompute.
  • Click Open Project Folder.
2. Pass command-line arguments

  • Right click MyCompute project node and select Properties.
  • Observe that the Project Properties dialog box appears.
  • Select Run under Categories section on the left.
  • For the Arguments field on the right, enter command line arguments, for example, 10 20, in this example.  (Figure-2.21 below)
  • Click OK.


Figure-2.21: Pass command line arguments

3. Build and run the project.

  • Right click MyCompute project node and select Run Project.
  • Observe the result in the Output window of the IDE. (Figure-2.13 below)


Figure-2.13: Result

Summary

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

 

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:
  • Receive the ages of the all your family members (between 3 to 6 members) as command line arguments in the format as following (name then age)
    • Monica 12 Daniel 34 Shelley 23
  • Compute and display the average of the ages that were entered.
2. Send the following files to javaprogramminghomework@sun.com with Subject as JavaIntro-commandarguments.
  • Zip file of the the MyOwnCommandLineArguments NetBeans project.  (Someone else should be able to open and run it as a NetBeans project.)  You can use your favorite zip utility or you can use “jar” utility that comes with JDK as following.
    • cd <parent directory that contains MyOwnCommandLineArguments directory> (assuming you named your project as MyOwnCommandLineArguments)
    • jar cvf MyOwnCommandLineArguments.zip MyOwnCommandLineArguments (MyOwnCommandLineArguments should contain nbproject directory)
  • Captured output screen  – name it as JavaIntro-commandarguments.gif orJavaIntro-commandarguments.jpg (or JavaIntro-commandarguments.<whatver graphics format>)
    • Any screen capture that shows that your program is working is good enough.  No cosmetic polishment is required.
  • If you decide to use different IDE other than NetBeans, the zip file should contain all the files that are needed for rebuilding the project – war file with necessary source files is OK.

 

 

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다

이 사이트는 스팸을 줄이는 아키스밋을 사용합니다. 댓글이 어떻게 처리되는지 알아보십시오.