The Java SDK

Many professional and student programmers do not use an integrated development environment but choose to use the Java Software Development Kit (SDK) and a text editor.

This setup has a number of advantages:

Installing the SDK

For Windows or Linux, you should download the Java SDK from http://java.sun.com/j2se and follow the installation instructions. When installing the SDK, you will be asked where you want to place the software. Typical choices are similar to

c:\j2sdk1.4 (Windows)
/usr/local/j2sdk1.4 (Linux)

The exact directory names depend on the SDK version. Make a note of the installation directories.

For Mac OS X, you need to download the developer tools from Apple (http://developer.apple.com/tools). The developer tools include the Java SDK.

Setting the PATH

After installing the SDK, you will want to add the location of the SDK program files to the PATH environment variable. That step is necessary so that the commands can be executed when you type them into a command shell, or when they are issued by a text editor with Java support.

For Windows NT/2000/XP, use the System Properties control panel setting to set the environment, as explained in http://java.sun.com/products/javamail/classpath-NT.html.

For Windows 95/98/ME, you need to edit the file c:\autoexec.bat. You can use Notepad to edit this file. Locate the line that starts with

PATH=some directories

You should add the directory c:\j2sdk1.4\bin and a semicolon immediately after the = sign, so that the PATH

statement reads:
PATH=c:\j2sdk1.4\bin;some directories

Note these points:

For Linux, you should edit the .bashrc file in your home directory. Add the line

export PATH=c:/usr/local/j2sdk1.4/bin:$PATH

at the end of the file.

Note these points:

If you use Mac OS X and installed the developer tools, then the path is already set correctly.

Testing your installation

You first need to open a command shell.

In Windows, follow these steps:

In Linux, locate an icon or menu option to start a terminal (sometimes called xterm). You will see a window like this:

.

In Mac OS X, follow these steps.

You will get a terminal such as this one:

.

Into the terminal window, type

java -version

You should get a response similar to

java version "1.4.1_01"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1_01-b01)
Java HotSpot(TM) Client VM (build 1.4.1_01-b01, mixed mode)

If instead you get a response such as

then there is an error in your installation. The most likely culprit is the PATH setting.

Editing Files

You can use any text editor to edit your files. On Windows, I recommend TextPad (http://www.textpad.com). A good choice for all platforms is JEdit (http://jedit.org).

After installing your editor, you should do some customization. In particular, you will want to adjust the tab stop setting to 3 tabs (the setting used in the textbook). It is also a good idea to tell the editor to use spaces instead of tabs.

These adjustments depend on the editor. For example, in TextPad version 4, choose Configure -> Preferences from the menu, then expand the tree on the left to Document Classes -> Java -> Tabulation.

.

Set the tab and indent settings to 3, and check the options to convert new and existing tabs to spaces. Click on Ok.

Other editors have similar settings.

Now is a good time to plan for a directory structure for your Java programs. It is best if you make a new directory for each program. For example, suppose that you want to organize your homework assignments.

In Windows, you may want to make a directory c:\homework and then place subdirectories hw1, hw2, etc. into it.

In Linux or Mac OS X, you can place the homework directory inside your home directory. The home directory is /home/yourname in Linux and /Users/yourname in Mac OS X.  In the command shell, you can abbreviate the home directory as ~.

You can create these directories either with a graphical tool (such as Windows Explorer or Mac OS X Finder), or simply in the command shell:

mkdir c:\homework
mkdir c:\homework\hw1
or
mkdir ~/homework
mkdir ~/homework/hw1

TIP: Keep both your text editor and your command shell window open at all times. In Windows and many versions of Linux, you can switch between the windows by hitting the ALT+TAB keystroke combination.

Compiling Programs

To compile a program, switch to the command shell window and change to the directory that contains the program:

c:
cd \homework\hw1

or

cd ~/homework/hw1

Then invoke the javac compiler and supply the name of your Java file. If your program consists of multiple Java files, simply supply the file that contains the class with the main method. For example,

javac MyProg.java

If the compilation is successful, then the file Myprog.class is created (possibly with other .class files).

If the compilation is not successful, then you will see some error messages on the screen.

.

The error messages tell you which lines have errors. You then need to switch back to the editor window, fix the errors, save your file, switch back to the compiler window, and run the compiler again.

If your program has many errors, then you may need to use the scroll bars on the command shell window to see the first messages. However, the command shell windows of older versions of Windows have no scroll bars. In that case, you probably want to compile programs inside your editor--see the next section.

Tip: On many command shells, you can hit the up-arrow key to retrieve commands that you already typed. Keep hitting the up-arrow key until you find the command that you want to repeat. Then hit the ENTER key to issue the command.

Compiling Programs inside the Editor

Many editors are configured (or can be configured) to compile Java programs automatically. The editor issues the javac command for you and captures the compiler output, making it easier for you to walk through the errors.

For example, the TextPad editor is has a keyboard shortcut CTRL+1 to invoke the Java compiler. The error messages are saved in a TextPad window. Double-clicking on an error brings you to the offending line in the program source.

.


Executing Programs

To execute a program, switch to the command shell window and change to the directory that contains the program.

Then invoke the java interpreter and supply the name of the class that contains the main method. Do not supply a .java or .class extension!  For example,

java MyProg

The program starts. If the program option panes or frames, they will pop up over the command shell window.

.

Many text editors also run the java command for you. For example, in TextPad, hit the CTRL+2 keyboard shortcut to execute the current program. Textpad starts a shell command window in which it runs your program.

.

Running Applets

To run an applet, switch to the command shell window and change to the directory that contains the program.

Then invoke the appletviewer program and supply the name of the HTML file that contains the applet tag for the applet.  Do not supply a .java or .class extension!  For example,

appletviewer MyProg.html

The applet viewer starts and displays your applet.

.

Some text editors also run the appletviewer command for you. For example, in TextPad, hit the CTRL+3 keyboard shortcut to execute the current program as an applet. Textpad makes an HTML file for you and starts the applet viewer.

Debugging

Most editors do not have a built-in Java debugger. The SDK has a command-line debugger (jdb), but it is very primitive. If you need a debugger, you can either use an integrated development environment such as Eclipse (http://eclipse.org) or a standalone debugger such as JSwat (http://www.bluemarsh.com/java/jswat/)


Creating Javadoc documents

To extract Javadoc comments from your source code, switch to the command shell window and change to the directory that contains the program code.

Then issue the command

javadoc *.java

The javadoc program generates the HTML documentation files for all classes in the current directory.

There are a number of useful Javadoc options.

The -link option lets you link to the Sun documentation for standard classes. Usage:

javadoc -link http://java.sun.com/j2se/1.4/docs/api *.java

The -d option lets you place the documentation into a separate directory. Usage:

javadoc -d docdir *.java

If you like to use a custom tag such as @precondition, then use the following command-line option:

javadoc -tag precondition:cm:Precondition: *.java

You can combine multiple options.