The JavaTM Tutorial
Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Trail: Getting Started
Lesson: Your First Cup of Java

Your First Cup of Java (for Mac OS)

Detailed Instructions
for Your First Program

The following instructions will help you write your first Java program. These instructions are for users of the Mac OS platform.

1. A Checklist 2. Creating Your First Application
     a. Create a Java Source File
     b. Compile the Source File
     c. Run the Program
3. Creating Your First Applet 4. Where to Go from Here

Your feedback is important to us! If you have comments about these instructions, send them to: tutorial@java.sun.com. In your message, put cup of java in the subject header. This page was adapted for Mac OS by tutorial reader Werner van Mook and updated by Lisa Fenwick.



1. A Checklist 

To write your first program, you will need:
  1. A development environment for the Java platform. You can download(outside of the tutorial)the Macintosh Runtime Environment for Java Software Development Kit (MRJ SDK) from Apple's website.
  2. A runtime environment for the same version of the Java platform. You can download(outside of the tutorial)the Macintosh Runtime Enviroment for Java (MRJ) from Apple's website.
  3. Stuffit Expander 5.5 to open these files. You can download(outside of the tutorial)it for free from Aladdin Systems' website.
  4. A text editor. In this example, we'll use SimpleText, the simple editor included with the Mac OS platforms. To find SimpleText, from the File menu select Find, type in "SimpleText", and then hit the Find button. You might find more then one SimpleText application; any one of them should work fine. You can easily adapt these instructions if you use a different text editor.
These four items are all you need to write your first Java program.


2. Creating Your First Application

 
Why Bytecodes are Cool

So, you've heard that with the Java programming language, you can "write once, run anywhere." This means that when you compile your program, you don't generate instructions for one specific platform. Instead, you generate Java bytecodes, which are instructions for the Java Virtual Machine (Java VM). If your platform--whether it's Win32, UNIX, Mac OS, or an Internet browser--has the Java VM, it can understand those bytecodes.

 
Your first program, HelloWorldApp, will simply display the greeting "Hello world!". To create this program, you will: 
  • Create a Java 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 edit source files.
     
  • Compile the source file into a bytecode file. The Java compiler, javac, takes your source file and translates its text into instructions that the Java Virtual Machine (Java VM) can understand. The compiler puts these instructions into a bytecode file.
     
  • Run the program contained in the bytecode file. The Java VM is implemented by a Java interpreter, java. This interpreter takes your bytecode file and carries out the instructions by translating them into instructions that your computer can understand.

top


a. Create a Java Source File.

You have two options:

  • You can save the file HelloWorldApp.java(in a .java source file) on your computer and avoid a lot of typing. Then, you can go straight to step b.

  • Or, you can follow these (longer) instructions:

1. Start SimpleText. In a new document, type in the following code:

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

Type all code, commands, and file names exactly as shown. The Java compiler and interpreter are case-sensitive, so you must capitalize consistently.

HelloWorldApp  helloworldapp

2. Save this code to a file. From the menu bar, select File > Save As. In the Save As dialog box: 
    • Specify the folder where you'll save your file. In this example, the folder is 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 this:

Now click Save, and exit SimpleText.

top


b. Compile the Source File.

Open the folder MRJ SDK 2.2 and it should look something like this:

Inside there is a folder called Tools. In this folder, there is a folder called JDK Tools.
Open it, and you will see a program called javac.

Now drag and drop your HelloWorldApp.java onto the Javac application. Javac will now open and should show you:

The text area Source Files shows the absolute path to 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 below is returned without error messages, congratulations. You have successfully compiled your program.


This figure has been reduced to fit on the page.
Click the image to view it at its natural size.

 
Error Explanation

If you drag and drop your .java file on top of the javac program and the file is only moved on top of the javac application.

When you tried to compile and the only thing that happens is that your .java file is copied/moved on top of your javac application you need to rebuild your desktop.

To rebuild, you have to restart you computer and press and hold both the Apple and Alt key until you get a dialog asking if you really want to rebuild your desktop.

Answer "Yes." When your desktop is finished rebuilding you should be able to drag and drop the .java file on to Javac.


The compiler has generated a Java bytecode file, HelloWorldApp.class. Look in the same folder where you saved the .java file and you will see the .class file.

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

top


c. Run the Program.

From the MRJ SDK 2.2 folder, open the Tools folder, then open the Application Builders folder. Finally open the JBindery folder which contains the application JBindery.

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 comes with the JBindery file. This is not the file you created.

You should now see the following dialog box:

Click the Run button. If you now get:

Congratulations! Your program works.

top


3. Creating Your First Applet

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

To create this applet, you'll perform the basic steps as before: create a Java source file; compile the source file; and run the program.


a. Create a Java Source File.

Again, you have two options:

  • You can save the files HelloWorld.java(in a .java source file)and Hello.html(in a .java source file)on your computer and avoid a lot of typing. Then, you can go straight to step b.

  • Or, you can follow these instructions:

1. Start SimpleText. Type the following code into a new document:

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

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

Save this code to a file called HelloWorld.java.

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

<HTML>
<HEAD>
<TITLE>A Simple Program</TITLE>
</HEAD>
<BODY>
Here is the output of my program:
<APPLET CODE="HelloWorld.class" WIDTH=150 HEIGHT=25>
</APPLET>
</BODY>
</HTML>

Save this code to a file called Hello.html.

Make sure that your files HelloWorld.java and Hello.html are in the same folder.


b. Compile the Source File.

Compile the HelloWorld.java source file using javac as before.

The compiler should generate a Java bytecode file, HelloWorld.class.


c. Run the Program.

Although you can view your applets using a Web browser, you may find it easier to test your applets using the simple Applet Runner application that comes with the JavaTM Platform. To view the HelloWorld applet using Applet Runner, Open the folder Apple Applet Runner in the MRJ SDK 2.2 folder. There should be an application called Apple Applet Runner.

Drag and drop your Hello.html file on this application.

Now you should see:

Congratulations! Your applet works.

top

4. Where to Go from Here

To continue your introduction to the Java programming language, check out these trails:

  Getting Started(in the Getting Started trail)
  Learning the Java Language(in the Getting Started trail)

top


About the Author

Werner van Mook transformed the original Win32 Cup of Java to this Macintosh version.
Werner is a Java technology instructor at Sun Microsystems in the Netherlands.
After seven years of delivering Macintosh support, he switched to the Windows platform and completed his MCSE+I in eleven months. Werner received a B. of Sc. in Informatics in December 1998. He is also a Certified Java Developer.


Updated 6/13/00 by Lisa Fenwick
Lisa worked with The Java Tutorial group as a summer intern at Sun Microsystems. She is a graduate student at Mills College in Oakland, California, where she expects to graduate with a Masters in Interdisciplinary Computer Science in May 2001.

Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Copyright 1995-2001 Sun Microsystems, Inc. All rights reserved.