Your first Java programs

April 3, 2001

Here are some suggestions for getting familiar with Java.

Demonstrate

Create a subdirectory for your Java programs. You might want to create a subdirectory for each Java program.

Use an editor to create a file Demonstrate.java that contains the Demonstrate class from page 10 in On to Java.

(There is also a copy of this program on grace in /usr/users4/fofc/java/winston.)

Compile the program:


   $ javac Demonstrate.java

Confirm that the compiler created a new Demonstrate.class file.

Run the program:


   $ java Demonstrate

   The rating of the movie is 23 

Hello world (etc.)

Create your own "Hello world"-style program that uses print or println to write messages on the screen.

Remember that the name of the file must be the same as the name of the class in the file.

Echo

Copy, compile and run the Echo program in /usr/users4/fofc/java/sample on grace.

This program shows how to read command line arguments, read from standard input, and write to standard output in Java.

You provide the input, by typing at the console or redirecting input from a file. Try different ways of running it:


   $ java Echo

   Hello

   ...



   $ java Echo "> "

   > Hello

   ...



   $ java Echo < infile

   ...

Client-server

Copy, compile, and run the Client and Server programs in /usr/users4/fofc/java/client-server on grace.

Run both programs on your own computer. Start each program in its own terminal window. A window will pop up for each. Press the START button on the Server window. In the Client, make sure the IP address is 127.0.0.1. Type port number 1234 into the Client, type a message and press SEND.

Use your Client to send a message to the Server running on the computer at the next desk. Its IP address should be on a label over the monitor screen.

Observe what happens in the Client terminal window if you try to send a message when your Server isn't running, or if you use the wrong port number or IP address.

Try the TermClient. Type the IP address and port number on the command line like this:


  $ java TermClient 127.0.0.1 1234

Then type messages into the terminal window.

Since TermClient runs in the terminal window, you can use it in a telnet session. You can telnet to grace, run TermClient and use it to communicate with a Server running on your computer at home.


Jon Jacky, jackyj@evergreen.edu