CSIS 3701: Advanced Object-Oriented Programming

Using the Java SDK

Introduction

Like most modern programming languages, Java is usually run in an environment of some sort, that allows for easy coding, compiling, and running. For this course, you have two options:

·        The default Java SDK (software development kit), available on the textbook CD-ROM or from Sun’s web site. This must be run from DOS.

·        The Forte environment, available on the textbook CD-ROM.

 

Downloading the Java SDK

Whether or not you decide to use Forte, you will first need to download the Java SDK (software development kit), which includes the java compiler and libraries.

Downloading from the CD-ROM

The simplest way to do this is to load it from the textbook CD-ROM.

  1. Load the CD-ROM
  2. Go to the jdk\windows folder.
  3. Execute the j2sdk1_3_0-win.exe file.

Downloading from Sun

The version of Java on the CD-ROM should be sufficient for anything we will do in this course. However, as with most software, Sun is constantly releasing new versions with more features and (hopefully) less bugs. If you like, you can always get the most current version from Sun’s web site:

http://java.sun.com/j2se/1.3/download-windows.html

  1. Skip past the ads to the section that reads "Download Java 2 SDK, v 1.3.0 Software for Windows 95 / 98 / 2000 / NT 4.0 (Intel Platform)", and choose either to download the entire package at once, or in disk size pieces. Downloading it at once is by far more convenient, and should be done unless you are downloading it on campus and installing it somewhere else.
  2. Before going any further, you should definitely follow the INSTALLATION link, and print out that page (it has lots of details like those I am giving here). When you return to this page, press "continue".
  3. This will take you to the standard licensing page, where you will want to accept the agreement. Feel free to read it first, of course -- some parts of the restrictions are actually kind of funny.
  4. This will take you to a list of download sites. You will probably want to just choose the first one and press that button.
  5. This should pop up a window that allows you to download the file j2sdk1_3_0-win.exe, an executable which installs the SDK. Downloading it may take a little while depending on the speed of your internet connection (it took about 5 minutes on campus), so this might be a good time to take a break...
  6. If you did not run the .exe, but downloaded it instead, you need to run it. Use Explorer (or DOS) to go to the directory where you downloaded it, and run the j2sdk1_3_0-win.exe file. You will see the usual things that happen when you install software (you will probably want to allow it to be installed in the default directory c:\jdk1.3).

 

Using the default DOS Java SDK

The default environment to compile and run programs in Java is DOS. This means that all of the below commands for compiling and running Java programs must be entered at the command line of a DOS window (just like all commands for compiling and running C programs in 2610 were entered at the command line of a UNIX window). If this is a problem – and it may well be in later DOS-hostile versions of windows -- you might consider using the Forte environment instead on your home computer. However, you should at least be familiar with how to edit, compile, and run Java programs in DOS if you wish to do so on the campus machines.

Editing, Compiling, and Running Java programs

Editing Java programs

There are a couple of good options for writing your code:

·        The DOS editor.
The DOS editor can be invoked at the DOS command line by entering edit filename, where filename is the name of the file. For example, to edit Hello.java, you would enter edit Hello.java. Keep in mind that all Java files must end in .java . The DOS editor has simple menus for saving, cut and paste, search, etc.

·        A simple Windows text editor, such as Notepad or Wordpad.
Note that by default, these programs will automatically tack a .txt or .doc at the end of your filename, which will confuse the Java compiler (all Java files must end in .java). To avoid this, do the following when saving:

o       If using Wordpad, save as type Text Document – MS-DOS Format

o       Put quotes around the filename, to force the editor to save it suing that exact file name. For example, to save Hello.java, type “Hello.java” as the file name to save as.

Compiling Java Programs

Java requires that the name of a file containing Java code have the same name as the main class in the file. For example, the
above code, which contains a class called Hello, would have to be stored in a class called Hello.java .

To compile an application called Hello.java, you would type the following at the DOS prompt:

javac Hello.java

If your code has no errors, this will produce a file called Hello.class .

Possible problems:

·        If you have lots of errors, some may scroll off of the screen. You can handle this by redirecting the output of the compiler to a file, and then using an editor to examine the contents of the file. This is done with the > operator (just like in UNIX). For example, to compile Hello.java while sending all error message to the file errors, type the following:

javac Hello.java > errors

·        If you try to compile, but get an error message of the form “Bad command or file name”, this means that DOS does not know where to find the javac command. See the below section on “Setting the PATH”.

·        If you try to compile a file, but get an error message of the form “error: cannot read”, this means that DOS does not know where to find one or more of the Java files that you are trying to compile. Keep in mind that the Java compiler automatically compiles any other classes that your class refers to – for example, if your Hello.java contains a reference to a class called Someclass, then the compiler will also try to find and compile Someclass.java before compiling Hello.java.

If that is not the problem, then the compiler may not know where to look for its Java files in the first place. See the below section on “Setting the CLASSPATH”.

Running Java Applications

Keep in mind that compiled Java .class applications are not executable files – instead, they contain platform-independent byte code, which must be interpreted before the program is run. The java command will do this, translating the code and executing the main function of the application.

For example, you can run the compiled Hello application using the command:

java Hello

This runs the application in the .class file.
 
Note that you may have to press control-c to shut the application down (we will show how to fix this later).
 
Possible problems:

·        If you get an error of the form “no main method”, then you attempted to run a class which does not have a main method (that is, something which is not an application).

·        If you get an error of the form ClassNotFoundException when running a program you have successfully compiled, then Java may not know where to look for its Java files in the first place. See the below section on “Setting the CLASSPATH”.

Setting up the Java SDK on your computer

As mentioned above, if you have problems running and compiling programs, then you may need to set the PATH and CLASSPATH environmental variables on your computer. Depending on you operating system, this will mean either modifying autoexec.bat or accessing the control panel. If you don’t want to do this, there are also “shortcuts” you can take which might also work, depending on your operating system, or you might consider using the Forte environment.

However, you may not need to do any of this. One way to find out is to run the following test:

·        Go to the examples section of my web page and copy the Sum.java and the MileageApp.java classes into files with the same name on your computer.

·        Compile MileageApp.java with the command javac MileageApp.java (this will also automatically compile Sum.java). If this works, you should now have the files MileageApp.class and Sum.class.

·        Run the application with the command java MileageApp (this executes the MileageApp.class file). If this works, an application window will appear on your screen.

Setting the PATH

In order for you to be able to compile and run programs, you will need to set some paths on your computer. Paths are simply the directories where your computer looks to find certain information, such as files and code for commands. These are generally set in your autoexec.bat file, if you are using Windows 98 or Windows 95, or in the control panel for Windows NT or Windows 2000.

If you attempt to compile your code, and get a message that the javac command could not be found, that means that your computer does not have a path to the directory where that command is stored, which is probably c:\jdk1.3\bin . To fix this, you will need to add the following path to your PATH list:

;c:\jdk1.3\bin

 

See the Sun installation page for a good explanation of how to do this.

Shortcut to avoid setting the PATH

If setting the path is a problem (and it can be in Windows 2000 and above), one quick shortcut is to always just write and compile your programs in the same directory as the javac compiler -- that is, c:\jdk1.3\bin.
 

Setting the CLASSPATH

Depending on your operating system, you may also need to set the CLASSPATH environmental variable. This variable lists all of the directories on you machine where the compiler and the run-time environment are to search for any files it needs – for example, if you compile or run the Java class Myclass.java, the compiler needs to know what directory to look for it in (it does not necessarily look in the current directory).

 

Your autoexec.bat file or control settings also determine where the javac compiler looks for the Java code files -- note that compiling a file while you are in its directory does not guarantee that javac will find it, particularly if it depends on other files. Javac will look for .java files in only those directories indecated in the CLASSPATH environmental variable.

For example, if you are writing your code in the directory c:\jdk1.3\myprogs, you would add the following line to your autoexec.bat file:

CLASSPATH: c:\jdk1.3\myprogs

You can list (separated by ;) as many directories as possible in your classpath. One important one, however is ".". This means that javac will always look in the current dircetory for the files it needs.

For example, to list the current directory, the myprogs directory, and the diskette drive as places where javac should look, you would have the following classpath:

CLASSPATH: .;c:\jdk1.3\myprogs;a:\

This environmental variable can be set the same way as the PATH above.

Shortcut to avoid setting the CLASSPATH

Again, if this is a problem, there is a shortcut you can use. The javac command has an option (the -classpath option) that allows you to manually specify the classpath during compilation. For example, to specify that the compiler should look in the current directory (that is, the "." directory) for the file Hello.java when compiling it, you would use the command:

javac -classpath . Hello.java

This flag will also work with the java command as well:

java -classpath . Hello
 

Using Meshel Hall labs

If you are using the PCs in labs 301, 302, or 303, you don't have to do any of this. All of these machines have their PATH and CLASSPATH variables correctly set. In addition, the CLASSPATH variable includes the a: drive, so your programs can be compiled and run directly from your diskette.

 

Using the Forte Environment

There are many visual environments for Java that work much the same way Visual Basic or Visual C++ do – that is, providing a single environment in which you can write Java programs. Forte (provided by Sun) is a free one that comes with our textbook.

The advantages of using an environment like Forte are:

·        It provides a built-in environment for editing, compiling, running, and debugging Java programs – that is, you don’t have to work from DOS.

·        It automatically handles any problems related to the PATH and CLASSPATH environmental variables.

Cautionary Note:

The Forte environment is also designed to automatically draw and generate code for visual components (much like VB and VC++) do. You are NOT to use these, but instead to write your code from scratch, using the steps given below.

The problem is that the code Forte generates is very cryptic and poorly designed, which would make it virtually impossible for you to debug or modify it. Once you are completely familiar with the Java language and concepts (that is, after this course), you may want to go back and try some of these features – but until then, they will make things more difficult for you, not less. 

Downloading from the CD-ROM

Note that you must install the Java SDK before installing Forte, as Forte will search for a Java environment to connect to.

  1. Load the CD-ROM
  2. Go to the forte folder.
  3. Execute the forte_ce_1_2_0.exe file.

 

Note: When Forte is being installed, you may be asked whether you want to make Forte the default program for all Java files. I recommend that you answer no. Otherwise, opening any Java file (including the examples on my web page) will automatically start up Forte, which can be very annoying.

 

Once you are done, you should have a shortcut to Forte on your desktop.

Starting new projects

Groups of classes that make up an application are called projects in Forte. As we will later see, these are actually referred to as packages in Java.

To start a new project on a diskette, do the following:

1.      Since you will be turning in your work on diskettes, you will need to do a little bit of setup before you start up Forte. Specifically, you will need to use Windows Explorer to create a subdirectory on your diskette where the project will be stored.

2.      Start up Forte (yes, this takes some time).
Note that if you have run Forte before, it will automatically bring up the last project you were working on.

3.      Select New Project from the Project menu. This will bring up the New Project window.

4.      Type in the name of the project. This should be the same as the name of the subdirectory that you created in step 1.

5.      If you are asked whether to start with a new file system, choose New.

6.      The Mount Project window will then come up. This is used by Forte to determine what directory you want to put the project in. Again, this should be the same as the name of the subdirectory that you created in step 1.

At this point, a message telling you the project was successfully opened should appear in the main Forte window.

Adding new classes to a project

Once you have a project opened, you can add new classes to it as follows:

1.      Select New From Template from the File menu. This will bring up the Template Chooser window.

2.      Double click on Classes in the list.

3.      Click on Empty in the sublist that appears, and press Next. This will allow you to add a new class completely free of any automatically generated code.

4.      Type the name of the class in the top of the window without the .java extension. Also make sure that the correct project is selected in the list below (it should be), and press Finish.

5.      You will be asked if you want to add the class to the project tab. Answer Yes.

At this point, a new empty window will appear with the name of the class. You can type in the code for your class here.

You can add as many classes as you want this way, using the project tab in the Explorer window to switch back and forth between them.

Compiling and Executing a project

Most things you will want to do can be found in the Project menu. For example, choosing Save Project from that menu will save all files in your project.

To compile all files in the project, choose Compile Project from that menu.

To execute the project, choose Execute Project from that menu (this will also recompile the project automatically if necessary). If this is the first time that you have run it, you may be asked which class is the main class (that is, the one with the static void main method).

If your project is a visual application, it will appear in a new window. In addition, if your program executes any System.out.println methods, they will appear in the Output Window at the bottom (any runtime errors will appear here as well).

Once you are done, you may need to press the Editing tab to bring your class files back up.