Using Java on Linux
Laboratory Exercise 3
CMP 420: Database Systems
Lehman College, City University of New York
27 February 2003
Unix Commands
Each lab begins with several Unix commands that will be useful
for completing today's lab and future labs and assignments.
Try the following commands:
- printenv
When you log-on on the lab machines, a working environment is
set up for you. Your environment includes your username, your
hostname, the operating system type, the default language (used
by help files and the keyboard), etc. To see your current
environment, type:
printenv
- grep pattern [filename]
The grep command lets you search for a pattern in
standard input or a file. For example,
printenv | grep PATH
searches the output from the printenv command for any
lines containing the word "PATH." If you don't specify exactly
where a program is that you would like to run, the computer first
checks in all the directories listed in your path.
- which commandname
When you type name of a program, the computer checks all the
directories in your path for that program to run. Often, you need
to know which version of the desired program the computer will
choose to use. To do this, use the which. For example,
if you would like to know which java compiler is used when you
type javac, you type:
which javac
- locate filename
The locate command searches the entire file system for
the filename. For example,
locate javac
gives the names of all files that contain "javac" in their complete
name. Sometimes, this can be a lot of files and will scroll off
the screen. For example, if you type:
locate java
you will see the 1900 files that contain "java" in their name.
Using Java
Today's lab focuses on using Java on the Linux systems. Installed on
the lab machines is the Java Developer's Toolkit (JDK 1.2.2), a standard
release from Sun Microsystems.
- Create a directory (or folder) for this lab.
mkdir lab3
(This command creates a new directory called ``lab3''.)
- Use this directory when creating any files for the
exercise. To move to that directory, type:
cd lab3
(This ``changes directories'' to p1.)
When you create files in the text editor, save them to your
subdirectory, lab3.
(To return to your home or main directory, type: cd .)
- Create a simple java program that will print Hello World!
as text. Call the class Hello and save the
file as Hello.java.
- To compile the file, type:
javac Hello.java
If there are errors, you will need to edit your program,
fix them, and compile again.
- To run your program, type:
java Hello
(The program should say "Hello World!")
Applets
Now, make an applet that will print "Hello World" in red letters on
a blue background. As in other systems, you will need to create
an HTML file for you applet. Compile your file as before and save
your HTML file as hello.html. To view your applet, you
can load the webpage into a browser (as you did on other systems), or
you can use a program designed for viewing applets, called
appletviewer. To use appletviewer, you type:
appletviewer hello.html
Add to your applet, 3 buttons that change the background color.
If you have forgotten some of your java, the API's on the
Sun webpages are at:
http://java.sun.com/products/jdk/1.2/docs/api/index.html.
Final Comments
Before leaving the lab, you need to log out and shut down your
computer. First, you should quit all applications. Then,
go to the K menu in the bottom left hand corner and
select Log Out. Choose the Shut Down option,
and shut off the monitor when you're done.
Remember to pick up any files you sent to the printer and to
take any personal belongings. It is difficult to get back
into the lab later (since it's locked when not in use), so,
it's worth making sure you haven't forgotten anything.