Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Detailed Instructions
for Your First ProgramThe 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 Program3. Creating Your First Applet 4. Where to Go from Here
Note: If you are using Mac OS X, consult Apple's Web site Mac OS X Java Runtime Environment for the latest information.This page was adapted for Mac OS by tutorial reader Werner van Mook and updated by Lisa Fenwick.
To write your first program, you will need:
1. A ChecklistThese four items are all you need to write your first Java program.
- A development environment for the Java platform. You can downloadthe Macintosh Runtime Environment for Java Software Development Kit (MRJ SDK) from Apple's website.
- A runtime environment for the same version of the Java platform. You can downloadthe Macintosh Runtime Enviroment for Java (MRJ) from Apple's website.
- Stuffit Expander 5.5 to open these files. You can downloadit for free from Aladdin Systems' website.
- 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.
a. Create a Java Source File.
You have two options:
- You can save the file
on your computer and avoid a lot of typing. Then, you can go straight to step b.
HelloWorldApp.java
- 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. } }2. Save this code to a file. From the menu bar, select File > Save As. In the Save As dialog box:
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
- 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.
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 theJavac
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 thejavac
program and the file is only moved on top of thejavac
application.When you tried to compile and the only thing that happens is that your
.java
file is copied/moved on top of yourjavac
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 toJavac
.
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.
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 calledHelloWorld.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.
3. Creating Your First Applet
HelloWorldApp
is an example of a Java application, a standalone program. Now you will create a Java applet calledHelloWorld
, which also displays the greeting "Hello world!". UnlikeHelloWorldApp
, 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
andHello.html
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.
topAlthough 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.
4. Where to Go from Here
To continue your introduction to the Java programming language, check out these trails:
Getting Started Learning the Java Language If you have comments about these instructions -- whether you loved them or had trouble following them -- please tell us.
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.
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2002 Sun Microsystems, Inc. All rights reserved.