Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
It's time to write your first application! These detailed instructions are for users of Mac OS X. Instructions for other platforms are in "Hello World" for Microsoft Windows and "Hello World" for UNIX.
Mac OS X includes the Java runtime environment software and developer tools, making it easy to get started with the Java programming language. This section gives you the information you need to compile and run most of the examples in this tutorial. You'll probably also want to read Apple's developer documentation for the Java platform, which you can find at http://developer.apple.com/java/.
To write your first program, you need:
- The latest version of Apple's Developer Tools for the Java platform. You can make sure you have the latest by visiting Apple's Java Downloads page. Make sure you download the developer tools, not just the runtime.
- The latest version of Apple's runtime for the Java platform. Thanks to automatic software update, you should already have this. However, you can doublecheck by visiting the Java Downloads page.
- A text editor. In this example, we'll use the editor in Xcode, a free IDE for Mac OS X. You can download the latest version of Xcode from Apple's Tools Downloads page. This section's instructions reflect Xcode 1.5.
These three items are all you need to write your first application.
Your first application,
HelloWorldApp
, will simply display the greeting "Hello world!". To create this program, you will:
- 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 edit source files.
- Compile the source file into a .class file. The Java compiler takes your source file and translates its text into instructions that the Java Virtual Machine can understand. The instructions contained within this file are known as bytecodes.
- Run the program. The Java launcher (
java
) uses the Java Virtual Machine to run your application.
With Xcode, you first create a project and then create or edit the source files in the project. Here are the exact steps you can follow to create an application implemented in a source file named
HelloWorldApp.java
.top
- Create a new project in Xcode. In the window that appears, scroll to the Java section and select Ant-based Application Jar (see the following figure).
- Type
HelloWorldApp
into the Project Name field (see the following figure) and click Finish.A project window comes up (see the following figure) to help you maintain your new HelloWorldApp
project. The project includes automatically generated files such asbuild.xml
andManifest
, which are used when compiling the source file and bundling the finished application, respectively. It also includes a directory namedsrc
that containsHelloWorldApp.java
.
- Click the src entry in the leftmost panel of the project window. You should now see the
HelloWorldApp.java
file created for your project.
- Pull the horizontal split pane from the bottom of the Xcode project window to reveal an editor, and then click (just once) HelloWorldApp.java. Now the file's contents appear in the editor, as shown in the following figure, and you can start editing the file.
- Edit
HelloWorldApp.java
so that it contains 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) { //Display "Hello World!" System.out.println("Hello World!"); } }
Be Careful When You Type Type all code, commands, and file names exactly as shown. The Java programming language is case-sensitive, so you must capitalize consistently.
HelloWorldApp helloworldapp
Once you've finished editing the source file, press the Build & Go button at the top of the project window. If the source file successfully compiles, the application will be executed and you should see a window with "Hello World!", as shown in the following figure:
Congratulations! Your program works.
If the program doesn't compile or compiles but doesn't run as expected, make sure you entered the source code exactly as shown in step 5. You might try saving
topHelloWorldApp.java
directly into your project'ssrc
directory, in case typos caused the problem. If that doesn't work, please see Common Problems (and Their Solutions).
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.