First Steps (MacOS)

The following detailed instructions will help you write your first program. These instructions are for users on MacOS platforms. (Users on Win32 platforms can find instructions on page 8. UNIX and Linux instructions are on page 16.) We start with a checklist of what you need to write your first program. Next, we cover the steps to creating an application and steps to creating an applet.

A Checklist

To write your first program, you need

  1. A development environment for the Java platform: You can download the Macintosh Runtime Environment for Java Software Development Kit (MRJ SDK) from Apple's Web site at this address: http://developer.apple.com/java/text/download.html
  2. A runtime environment for the same version of the Java platform: You can download the Macintosh Runtime Environment for Java (MRJ) from Apple's Web site: http://developer.apple.com/java/text/download.html
  3. Stuffit Expander 5.5 to open these files: You can download this program from Aladdin Systems's Web site at this address: http://www.aladdinsys.com/expander/expander_mac_login.html
  4. A text editor: In this example, we'll use SimpleText, the basic text editor included with the MacOS platforms. To find SimpleText, from the File menu, select Find, type SimpleText, and click the Find button. You can easily adapt these instructions if you use a different text editor.

Creating Your First Application

Your first program, HelloWorldApp, will simply display the greeting "Hello World!" To create this program, you complete each of the following steps.

  • Create a source file. A source file contains text, written in the Java programming language, that you and other programmers can understand. You can use any text editor to create and to edit source files.
  • Compile the source file into a bytecode file. The compiler takes your source file and translates the text into instructions that the Java Virtual Machine can understand. The compiler converts these instructions into a bytecode file.
  • Run the program contained in the bytecode file. The Java interpreter installed on your computer implements the Java VM. This interpreter takes your bytecode file and carries out the instructions by translating them into instructions that your computer can understand.

Create a Source File

graphics/intfig04.gif

To create a source file, you have two options. You can save the file HelloWorldApp.java [1] on your computer and avoid a lot of typing. Then you can go straight to the second step of compiling the source file (page 26). Or, you can follow these longer instructions.

[1] Throughout this book, we use the CD icon to indicate that the code (in this case, HelloWorldApp.java) is available on the CD and online. See Code Samples (page 43) for the file location on the CD and online.

First, start SimpleText. In a new document, type in the following code:

/** 
 * The HelloWorldApp class implements an application that 
 * displays "Hello World!" to the standard output. 
 */ 
public class HelloWorldApp { 
 public static void main(String[] args) { 
 // Display "Hello World!" 
 System.out.println("Hello World!"); 
 } 
} 

Be Careful When You Type

Type all code, commands, and file names exactly as shown. The compiler and interpreter are case sensitive, so you must capitalize consistently. In other words, HelloWorldApp is not equivalent to helloworldapp.

Second, save this code to a file. From the menu bar, select File > Save As. In the Save As dialog, do the following.

  • Specify the folder where you'll save your file. In this example, the folder is called MRJ SDK 2.2.
  • In the Save this document as: text box, type HelloWorldApp.java.

When you're finished, the dialog box should look like Figure 17. Now click Save and exit SimpleText.

Figure 17. Saving the HelloWorldApp.java file.

graphics/01fig17.gif

Compile the Source File

Open the folder MRJ SDK 2.2 (or whatever you have named your folder); it should look something like Figure 18.

Figure 18. The contents of the MRJ SDK 2.2 folder.

graphics/01fig18.gif

From the MRJ SDK 2.2 folder, select Tools > JDK Tools. This last folder contains the program javac.

Figure 19. The javac icon.

graphics/01fig19.gif

Now drag and drop your HelloWorldApp.java file onto the javac application. The javac application will open and should look like Figure 20.

Figure 20. The result of dropping the file HelloWorldApp.java onto the javac application.

graphics/01fig20.gif

The Source Files text area shows the absolute path of the .java file we just created. Now there's nothing left to do except click the Do Javac button to compile your code. If a message like the one shown in Figure 21 appears without error messages, congratulations. You have successfully compiled your program.

Figure 21. The result of a successful compilation of HelloWorld.java.

graphics/01fig21.gif

The compiler has generated a Java bytecode file, HelloWorldApp.class. Look in the same folder where you saved the .java file to locate the .class file (Figure 22).

Figure 22. After you compile HelloWorldApp.java, the bytecode file, HelloWorldApp.class, is created in the same folder.

graphics/01fig22.gif

Now that you have a .class file, you can run your program.

Run the Program

From the MRJ SDK 2.2 folder, select the Tools > Application Builders > JBindery. The JBindery folder contains the JBindery application (Figure 23).

Figure 23. The JBindery icon.

graphics/01fig23.gif

Drag and drop the HelloWorldApp.class file in the MRJ SDK 2.2 folder on top of the JBindery icon.

Note

A file called HelloWorld.class is included with the JBindery file. This file is not the one you created.

You should see the dialog shown in Figure 24.

Figure 24. The result of dropping the HelloWorldApp.class file onto the JBindery program.

graphics/01fig24.gif

Click the Run button. Figure 25 shows what you should see.

Figure 25. The result of running the HelloWorldApp application.

graphics/01fig25.gif

Congratulations! You have just run your first program.

Creating Your First Applet

HelloWorldApp is an example of an application, a standalone program. Now you will create an applet called HelloWorld, which also displays the greeting "Hello world!" Unlike HelloWorldApp, however, the applet runs in a Java-enabled Web browser, such as the HotJava browser, Netscape Navigator, or Microsoft Internet Explorer.

To create this applet, you'll perform the basic steps as before: create a source file, compile the source file, and run the program. However, unlike for an application, you must also create an HTML file.

Create a Source File

graphics/intfig04.gif

You have two options to create a source file. You can save the files HelloWorld.java and Hello.html [1] on your computer and avoid a lot of typing. Then you can go straight to the second step of compiling the source file (page 31). Or, you can follow these instructions.

[1] HelloWorld.java and Hello.html are available on this book's CD and online. See Code Samples (page 43).

First, start SimpleText. In a new document, type the following code:

import java.applet.Applet; 
import java.awt.Graphics; 

public class HelloWorld extends Applet { 
 public void paint(Graphics g) { 
 // Display "Hello world!" 
 g.drawString("Hello world!", 50, 25); 
 } 
} 

Save this code to a file called HelloWorld.java.

Second, you also need an HTML file to accompany your applet. Type the following code into a new SimpleText document:


 

A Simple Program

Here is the output of my program:

Save this code to a file called Hello.html. Make sure that your files HelloWorld.java and Hello.html are in the same folder.

Compile the Source File

Compile the HelloWorld.java source file using javac as before. The compiler should generate a bytecode file, HelloWorld.class.

Run the Program

Although you can use a Web browser to view your applets, you may find it easier to test your applets by using the simple Applet Runner application that comes with the Java platform. To view the HelloWorld applet using Applet Runner, select Apple Applet Runner in the MRJ SDK 2.2 folder (Figure 26).

Figure 26. The Apple Applet Runner icon.

graphics/01fig26.gif

Figure 27 shows what you should see.

Figure 27. The successful execution of the HelloWorld applet.

graphics/01fig27.gif

Congratulations! Your applet works. If you encounter errors, see Common Problems and Their Solutions (page 391) to help you fix the problems.

Error Explanation (MacOS)

If you drag and drop your .java file on top of the javac application and the file is only copied or moved on top of the javac application, you need to rebuild your desktop. To rebuild, you must restart you computer and press and hold the Apple and Alt keys until a confirmation dialog appears. Answer "yes" to the question asking whether you want to rebuild your desktop. When the rebuilding of your desktop is finished, you should be able to drag and drop the .java file onto javac to compile your program.

Getting Started

Object-Oriented Programming Concepts

Language Basics

Object Basics and Simple Data Objects

Classes and Inheritance

Interfaces and Packages

Handling Errors Using Exceptions

Threads: Doing Two or More Tasks at Once

I/O: Reading and Writing

User Interfaces That Swing

Appendix A. Common Problems and Their Solutions

Appendix B. Internet-Ready Applets

Appendix C. Collections

Appendix D. Deprecated Thread Methods

Appendix E. Reference



The Java Tutorial(c) A Short Course on the Basics
The Java Tutorial: A Short Course on the Basics, 4th Edition
ISBN: 0321334205
EAN: 2147483647
Year: 2002
Pages: 125

Similar book on Amazon

Flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net