The purpose of this page is to outline one process students enrolled in CS courses can follow when creating Java programs, whether in lab or for projects. The approach outlined here makes use of "command line" Java tools found on Macintoshes running OS X. (Another approach is to make use of an integrated development environment (IDE) such as BlueJ, JJEdit or NetBeans. NetBeans is introduced in CPS122).
From time to time, it becomes necessary to do some fine-tuning of the procedures. Therefore, you should check this page periodically to be sure you have the latest information. (Check the revision date at the bottom to see when this file was last changed).
Before proceeding with the steps outlined here, it is first necessary to login to the computer and connect to the file server. The steps needed for doing this are outlined on a separate page. (These steps are the same, regardless of what software development tools one is using.)
Creating a Java program involves three distinct steps:
The tools you need to accomplish this task are installed on each machine in the Mac Lab. TextWrangler - the editor we will use for source program creation - is a graphical editor. The Java Compiler (javac) and runtime (java) are tools accessed from the command line.
Note that all of the files you will use with these tools will reside on your server volume.
To access TextWrangler, you simply need to click its Icon in the dock.
Once the window opens, you will need to enter the a command to set your default directory correctly. (For more information on command line commands, click here)
Suppose, for the sake of discussion, that the folder in which you are working is called Lab1 and resides at top-level in your server volume. Then the necessary command would be:
cd /Volumes/serverVolumeName/Lab1 (Where serverVolumeName is the name of your server volume)
Note, in the figure above, how the command line prompt specifies the name of the current working directory. Note, too, that, if the folder you wish to use is a subfolder of some other folders, then all the folders need to appear in the path you set with cd - e.g. if you wish to work with files in a folder called Lab1 which resides in a folder called labs which resides in a folder called CS211 which resides in a folder called courses on your server volume, then the correct cd command would be: cd /Volumes/serverVolumeName/courses/CS211/labs/Lab1
We will use TextWrangler in two ways:
Note that, when you are working on a program, you can keep a copy of the source file open in TextWrangler while you compile and/or run the program. If you discover a need to make changes, make the change in the open copy and don't forget to save your changes before recompiling your program!
javac sourcename.java
(e.g. if you were compiling a file named Lab1.java then the command would be javac Lab1.java.)
If there are any syntax errors in your program, one or more error messages will be printed on the screen after your command. If you simply see the prompt printed, without any intervening error messages, then your program compiled successfully. (No news is good news :-) ). Of course, if javac reports any errors during compilation, you must correct them using TextWrangler and then recompile the program.
java classname
(e.g. if you were running a program whose main() method is in a file named Lab1.java then the command would be java Lab1.)
Note that you do not include ".class" as part of the file name you specify for the
java
command, though you do include ".java" as part of the file name you
specify to the javac
command.
If your java program creates a window on the screen, the window will appear shortly after you issue the command to run the program. Output that is written to System.out will be sent to the terminal window, and any input read from System.in should be typed in that window.
You can terminate your program in one of three ways:
package
statement, and therefore all will reside in the
default "top-level" package. This is the case we will discuss here.
(See the page entitled Working with Multiple Packages
for the case where program files reside in multiple packages.)
In brief, when a program consists of multiple classes, each in its own source file,
then you must compile eachsource file with javac
, but you run the main
filewith java
. Thus, for example, if a program consisted of three
classes Foo, Bar, and Baz, residing - respectively - in Foo.java, Bar.java, and Baz.java,
with the main()
method being in class Foo, then the following sequence
of commands would compile all the classes and run the program:
javac Foo.java
javac Bar.java
javac Baz.java
java Foo
Actually, the java compiler helps you out a bit with needing to explicitly compile
source files. If you compile a file, and it depends upon another file has not yet
been compiled (no .class file exists), then the compiler will compile that file as well.
Thus, for example, if class Foo uses class Bar, and class Bar, in turn, uses class Baz,
then only the first javac
command above would be needed, because the
other two classes would be compiled as well when compiling class Foo. Thus, in most
cases, it suffices to compile the main program and all the other source files will
be compiled as well, since the main program presumably depends (directly or indirectly) on all
the other classes.
Of course, having once compiled the three classes, you can use the java
command to run the program as often as you wish. Likewise, if you make changes to one
of the classes - say Baz.java - then you may only need to recompile that class with
javac
before running the program again. However, if either Foo or Bar
is affected by the changes made to Baz, then you would need to recompile them as well.
Again, the java compiler helps you out a bit here. If you recompile a file that depends on another class, and the source for that class has been modified since it was last compiled, then the other class is compiled as well. However, the compiler only detects this "one deep" - e.g. say you modify Foo and Baz (but not Bar); suppose further that Foo depends only on Bar and Bar depends only on Baz. Suppose, further, that the change to Baz requires Bar to be recompiled as well (e.g. the value of a constant is changed, or the signature of a method is changed). Then you would have to explicitly recompile Bar as well, because the compiler would not detect the need to do so when recompiling Foo.
A final note: if you need to recompile many source files in the same folder, it may
be easier to use a single javac
command with a wild-card - e.g.
javac *.java # recompiles all .java files in the current directory.