Getting Started with Java
1

It is imperative that all your Java applications (packages) be stored in separate directories). I recommend that you keep all your Java files below the C:\cis206 directory (you may wish to substitute C for other drive). To create this directory, do this:
Select the drive's root directory (C:).
Next, click File, New (), Folder () options.

...


2

Type cis206 and press .


3

You will next create another folder (test) inside the cis206 folder. Do this:
Select the the cis206 folder.
Right-click the folder's contents.
From the pop-up menu, select option New.
Finally, select option Folder.

...


4

Type test and press .
Double-click the folder to get in.

...

5

You will now develop a Java program (SumOf.java) using a text editor. Make sure that your C:\cis206\test folder is open and do this:
Right-click the folder's contents.
From the pop-up menu, select option New.
Finally, select option Text Document.


6

Right away type SumOf.java over the default name and press . Click Yes to confirm your action.


7

Now, open the new file in a text editor (Notepad or EditPad)**.
Right-click the file.
Select Send To.
Select EditPad Lite (or other text editor)***.


8

Type the following Java program (another text editor can be used):


9

Save the program () and open the Command (DOS-Prompt) window (). If you do not have a shortcut to the Commander in your Quick menu, execute this: Start > Run > command .


10

Try the following commands:
Change the working directory to C:\cis206\test: cd \cis206\test
Compile your program: javac -classpath . SumOf.java
[If you get compilation error messages, go back to your editor, find the source of the error and fix it.]
Run the program to calculate the sum of 1 and 2: java -cp . SumOf 1 2
Run the program to calculate the sum of 4.75 and 7.25: Java -cp . SumOf 4.75 7.25
Try an incomplete sum: Java -cp . SumOf 10
[Java expects two argument. If you supply only one argument the array arg (see code) will contain one entry (at index 0). An attempt to access the second one (at index 1) will fail, since there is no such entry. Thus ArrayIndexOutOfBoundsException! This is an example for a run-time error.]
Try a sum of more than two numbers: Java -cp . SumOf 10 20 30
[The sum of only two first is returned. The third one is ignored, since the program only takes the first two! This is an example for a logical error. The program has not crashed but it failed to return a correct output.]

Type exit and press to close the Command widow.

11

In your next exercise, you will create batch files to simplify compilation and execution of your program. A batch file is a text file that may contain commands and control statements. It can be thought of as a shortcut. In Windows, batch files have the .bat extension.

Switch back to Windows Explorer and create another text document.

...


12

Type comp.bat, press , and click Yes.

 

13

Right-click the batch file () and go for Edit ().


14

The the command to compile your program:


15

Save the file:


16

You will now recycle this file to create another batch file (exec.bat). Modify the command so that it will look like the following one. Note that %1 and %2 represent arguments that will be supplied at the command line.


17

Invoke the File > Save As command (not File > Save):


18

Change the name of the file to exec.bat and click Save.


19

Close the editor.


20

Open the Command window again.


21

Perform the following commands:
CD \cis206\test
comp
exec 1 2


22

Here is how it works with the exec.bat command. The exec 10 20 command invokes the exec.bat command, replacing the argument %1 with a value of 10 and the argument %2 with a value of 20., thus creating command:
   Java -classpath . Sum of 10 20
that is carried out.


23

Type exit and press to close the Command widow.


This is it.

*** If there is no text editor in your Send To folder, all you have to do is to create a shortcut of your preferred editor there. In Windows 98, the Send To folder is located in the C:\Windows directory. In Windows XP/2000, you will find such folder below the C:\Documents and Settings\UserName directory****.
To add a shortcut of a program to the Send To folder, first locate the program (e.g. Notepad is usually in the C:\Windows directory), next make sure that both the program and the Send To folder are revealed, finally right-drag the program and drop it on the Send To folder. From the menu, select the Create Shortcut Here option.
Return
**** If you can't find the Send To folder, it means that it is a hidden folder. In Windows Explorer, go to
Tools > Folder Options > View
and turn on the Show hidden files and folder option.