Notes on Java


Contents:


Compiling Java programs

Run the Java compiler as follows:
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 under 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) $<


Web information on Java


Java books


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


Created: September 1, 2000
Last Modified: August 11, 2009