This is a MIRROR of T.A. Ken Been's notes


Running Java Programs

You may use any system for writing and testing your java programs for this class. However, you must use only standard java; do not use any non-standard extensions, such as those provided in the Microsoft development tools. That way, I will be able to test your programs on my system. In fact, I will be using jdk1.1.5, so if you are using that, or any jdk1.1.x, there should be no problem. Java is installed on omicron; you can do your assignments there, if you like. (If you do ``java -version'' it will say what version it is. Since I don't have an account on omicron, I don't know.) If you need an account on omicron, you can contact Robin Simon, in the computer science department.

Java programs are run as follows. (These instructions are for unix, with the Sun jdk installed. If you are using a different system, you are on your own!) First, suppose you write a java application (not an applet), and the entire program is in one class called MyProgram. Then this class should be in a file called MyProgram.java. You compile the program to byte code with the command

 javac MyProgram.java 
This will create a file called MyProgram.class. You can run the program by saying
 java MyProgram 

More commonly, you will have several classes in several files. Suppose the main() function is in class MyProgram, which is in file MyProgram.java, as above, and this program also uses classes X, Y, and Z, in files X.java, Y.java, and Z.java. Then you must compile all the files to byte code with

 javac MyProgram.java X.java Y.java Z.java 
or, more simply,
 javac *.java 
Then you run the program the same way as above:
 java MyProgram 
because MyProgram is the class with the main() function in it.

You can put more than one class in a file, but I find it easiest to just make a separate file for each class. Then if the class is named XXX, the file should be named XXX.java. That is a pretty standard way of doing it.