Using BlueJ as our Java development platform has offered us many conveniences. It provides a nice, graphical user interface which integrates the control and editing of the source code files, a compilation process in which errors are highlighted in the original source code, and the ability to create and inspect objects interactively or to execute a program via the debugger. There are many other such IDEs (integrated development environments) which offer similar conveniences.
In actuality, BlueJ is software which is sitting on top of a lower-level Java Software Development Kit (SDK) which is provided by Sun Microsystems.
Our goal at this point is to see how to live without BlueJ and instead to rely directly on the SDK when working with a "command line prompt" in a "terminal" window. There are three distinct issues to address towards this end:
On patel2, you can open up a terminal window by clicking on an icon of an old-fashioned computer monitor at the bottom of the screen (it is the fourth icon from the left). Alternatively, you can go to the start menu, the System submenu, and select Konsole (Terminal Program). At this point, a new window should appear allowing you to type in commands at what is known as the "command line prompt" or sometimes known as the "shell prompt."
Bare Bones Linux commands:
pwd
prints the location of your current working directory
ls
lists the contents of the current directory. This will include
both files as well as subdirectories. On patel2, the files'
names are drawn in black and subdirectories are drawn in blue,
though this will vary on other systems.
ls -F
lists the contents of the current directory, appending each
directory name with a suffix "/" (as well as other suffixes for
other types of files). This is sometimes helpful if you cannot
rely on the color-coding mentioned above.
cd
changes your working directory (by default back to your home directory)
cd dirname
changes your working directory to the specified dirname
cat filename
prints the contents of the specified filename.
more filename
similar to cat however it provides a way to walk
through the file more slowly. The user can press the spacebar
to advance one screen at a time, press the enter key to advance
one line at a time, or press 'q' to quit.
mkdir dirname
makes a new (empty) directory with the specified dirname
mv currentfile newlocation
moves the specified currentfile to the
newlocation. This command can be used to essentially
rename a file while leaving it in the same working directory, or
to actually move a file to some other directory.
rm filename
removes the specified filename. (Warning: make
sure you are careful, because it will not necessarily ask for
confirmation and does not rely on a Trashcan to recover files)
The source code for each class is stored in a separate file. The name of that file must be the name of the class followed by the .java suffix.
javac filename
Invokes the Java compiler on the specified filename
which should have the .java suffix. In addition to
compiling this one class, it will automatically compile any
other classes on which the specified class depends.
If a compilation is successful, there will exists a new file for each class, with a filename equal to the class name followed by the .class suffix.
java classname
This begins the execution of a program. In particular it
executes the main method of the specified class. We
discuss the significance of this method below. Please note
that the specified classname should indeed be the
precise classname (without any associated suffix).
BlueJ offered us the convenience of a point-and-click way to begin the execution of any class method. When working at the command-line, we do not have this flexibility. In particular, the only method we can execute directly is a class method with the following precise declaration:
public static void main(String[] args) {
}
This is the so called main method for a class. Notice that
the keyword static is used to designate this as a class
method; this allows it to be executed even in advance of the
existence of any objects from the class. The method does not have
any return value. You may also note that the method accepts a single
parameter, which is an array of Strings. These strings are commonly
called the "arguments" or "command line arguments."
For example, let's assume that we have a class named Example with such a main method. Then we could execute that main method by typing the following at the command line prompt:
java ExampleIn this case (the default), the array args is actually an array of length zero. However, the user can also specify any number of additional arguments on the command line, with whitespace used to separate such arguments. Therefore, if the user types the following:
java Example bob 5 how_are_youThe array args will be initializes with length 3, and with the three strings {"bob", "5", "how_are_you"}.
The purpose of allowing command line arguments is for convenience in specifying some controlling parameters for programs. Of course programs can always be written so as not to rely on command line arguments, but instead to prompt the user to enter information as the program is running.