站内搜索: 请输入搜索关键词
当前页面: 在线文档首页 > Java Tutorial 5.0 英文版

"Hello World" for Mac OS X - Java Tutorial 5.0 英文版

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: The "Hello World" Application

"Hello World" for Mac OS X

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 (in the Getting Started trail) and "Hello World" for UNIX (in the Getting Started trail).

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/ (outside of the tutorial).


A Checklist 

To write your first program, you need:
  1. 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 (outside of the tutorial) page. Make sure you download the developer tools, not just the runtime.
  2. 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 (outside of the tutorial) page.
  3. 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 (outside of the tutorial) page. This section's instructions reflect Xcode 1.5.

These three items are all you need to write your first application.


Creating 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.

top


Create a Source File

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.

  1. 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).

  2. 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 as build.xml and Manifest, which are used when compiling the source file and bundling the finished application, respectively. It also includes a directory named src that contains HelloWorldApp.java.

  3. Click the src entry in the leftmost panel of the project window. You should now see the HelloWorldApp.java file created for your project.
  4. 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.

  5. 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!");
        }
    }
    
  6.  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

top

Compile and Run

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 HelloWorldApp.java (in a .java source file) directly into your project's src directory, in case typos caused the problem. If that doesn't work, please see Common Problems (and Their Solutions) (in the Getting Started trail).

top

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

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