Notes on Java


Contents:


Compiling Java programs

To compile a Java program on one of the Computer Science machines, you first need to add to your PATH. Do this by editing your ~/.kshrc (after you change this file, you need to run the command ".  ~/.kshrc", or you can log out and log back in again). Once your path is set correctly, you should be able to run the Java compiler:
This will cause the compiler to compile YourFile.java as well as any other .java files that the code in YourFile.java uses. If the compile succeeds, it will produce a .class file for every class that was defined in YourFile.java.

Note: Remember that Java wants the name of the class to match the name of the file (this is especially important for public classes, like applets). Also remember that undex unix, the case of file names is significant - this means the case of the class name and the case of the file name must match.

Once you have compiled your Java program, you can run it using the Java interpreter:

Note that no .class nor .java extension is used in the above line. The above command will call the method in the class called YourClass.

Compiling Multiple Files

If you have several .java files that make up a program, you can often get away with just compiling the `top level' program - the compiler will notice the classes (source files) used by this and re-compile them as needed. While the above works in most simple cases, it doesn't always work. If you have problems with it, you can specify several .java files on the javac command line and the compiler will compile all of them. Alternatively, you can create a Makefile to do the job:
	CLASSFILES=AClass.class BClass.class # ...
	JAVAFLAGS=
	JAVAC=javac

	all: $(CLASSFILES)

	.SUFFIXES: .java  .class
	.java.class: ; $(JAVAC) $(JAVAFLAGS) $<

Where to compile

The best machines to compile and run Java programs on are the ones running Freebsd (e.g., phobos, lyman or the machines in the graphics lab) or ones running Linux (e.g., the ones in the EN-2036 or in EN-1049). The reason for this is these machines have Java 1.1.7 installed on them; garfield currently has version 1.02, which does not support some of the things we will be covering (i.e., some of the example programs I'll be covering won't compile on garfield).


How to use jdb (the Java debugger)

The debugger for java is called jdb - to use it properly, you need to have compiled your programs with the -g (debugging) option. The debugger is a bit primative (no GUI, no command line history, no command aliases), but appears to work in this version (versions that come with 1.0.x didn't). A breif intro to the debugger can be found in the JDK 1.1.7B documentation tool document's section, under jdb.


Web information on Java Web


Java books


This page is adapted from that for CS3710 (Winter 1999) created by Mike Rendell.


Created: January 17, 2000
Last Modified: March 7, 2000