1.3 Developing Java Programs

There are a number of ways to create and run a Java program. If you are working with Java on your own, you should feel free to choose the one that suits you. If, as is more likely, you are in a computer science course, your instructor may specify the way that the local system operates. Assuming that you are free to choose a program development environment, here is a brief outline of the choices available.

Almost everything that you will need to develop Java programs is available for free from Sun Microsystems, the developers of Java. To obtain a copy of Java from Sun, go to their web site at http://java.sun.com and download the Java 2 Standard Edition (J2SE) Software Development Kit (SDK) appropriate for your computer's operating system. The SDK contains a variety of tools, including a compiler, an interpreter, and the Application Programming Interface (API) - a huge library of classes containing methods that you will be using in your programs. You may want the latest version of the SDK but, if your institution is using an earlier one, it is probably best to get that (to be sure that programs developed at home will also run when marked at school). To use the SDK directly, we must operate in a text-based environment, without using a mouse. We will examine the details of this process shortly.

As an alternative, there are many versions of programs called Integrated Development Environments (IDE's) that provide, as the name suggests, an environment that is designed to assist the user in developing Java programs. An IDE can help you in organizing files, writing programs, finding mistakes in programs, and running programs - using both a mouse and the keyboard. Some IDE's come complete with all tools necessary for program development while others require that the SDK be obtained from Sun. Some are free; others are quite expensive. If you are interested in using an IDE, a search engine on the web pointing to "Java IDE" will get you a great deal of information about what is currently available.

To start the development of a program, you must first write the source program using some text editor. If you are using an IDE, then this will be part of the IDE. If not, then you can use one of the text editors available with your operating system. As examples, Windows has a text editor called Notepad; UNIX systems have a number of editors, including pico, vi, and emacs; and X-Windows has nedit and xedit. It is better to use a text editor rather than a word processor since we want to create a simple text file while word processors tend to produce files that include information about layout, fonts, and so on. The program must be saved in a file with a name of the form (Throughout this book, items printed .in a font like this and surrounded by angle brackets are used to indicate forms. Actual values are obtained by substituting something of the required form, without the angle brackets. Anything not contained in angle brackets should appear exactly as shown.)

<name>.java

where <name> is exactly the same name that was used for the class. The .java part is called an extension. As an example, the source file for a program contained in a class called Sample should be called Sample.java (with the case of the letters matching exactly).

Once the program has been written and saved, it must then be compiled into Java byte code. If you are working with an IDE, then a mouse click should invoke the Java compiler. If you are working in a command window, the compiler can be invoked for the program in the file called Sample.java by writing

   javac Sample.java

Before converting the program to byte code form, the compiler first tries to find mistakes in the program. It cannot find all mistakes but it is quite clever and it can detect many of them. If errors are detected during compilation, a message will be printed and compilation will not be completed. If this occurs, the programmer must analyze the problem, correct the source code, and repeat the attempt to compile the program. With a complex program, this process may be repeated many times before a program compiles without errors.

Once the compiler is happy with the form of the program, it will compile it to produce a byte code file called <name>.class. At this point, we must call upon the Java Virtual Machine (JVM) to run the program by interpreting the byte code and executing the program's instructions. In an IDE, another mouse click should allow you to do this. In a command window, the JVM can be invoked by writing

   java <name>

As an example, for the program originally created in the file Sample.java and compiled into the file Sample.class, we would run the program by writing

   java Sample

Notice that there is no extension on the file name in this command.

Even if a program compiles correctly, it may not run correctly. If this is the case, then once again the problem must be analyzed and the program must be corrected in the editor, re-compiled, and re-run. This cycle, sometimes called the program development cycle, is illustrated in the next diagram.

Exercises 1.3

  1. Copy and run the program shown in Example 1 on page 6.

  2. To get some familiarity with Java's reactions to mistakes in a program, try introducing the following errors (one at a time) in the program of Question 1 and then attempting to compile and run the resulting program. Note the result in each case.
    1. Change Greet to greet.
    2. Change main to Main.
    3. Change println to write.
    4. Add a semi-colon after args).
    5. Omit the square brackets after the word String.