Java 教程是为 JDK 8 编写的。本页中描述的示例和实践未利用在后续版本中引入的改进。
现在是编写第一个应用程序的时候了!These detailed instructions are for users of Solaris OS and Linux. Instructions for other platforms are in "Hello World!" for Microsoft Windows and "Hello World!" for the NetBeans IDE.
如果你在本页面上遇到问题,请参阅 Common Problems (and Their Solutions)。
要编写第一个程序,你需要:
The Java SE Development Kit 8 (JDK 8)
You can download the Solaris OS or Linux version now. (请确保你下载 JDK,不是 JRE。)请参阅 installation instructions。
一个文本编辑器
In this example, we'll use Pico, an editor available for many UNIX-based platforms. You can easily adapt these instructions if you use a different text editor, such as vi
or emacs
.
这两项是你编写第一个应用程序所需的全部内容。
你的第一个应用程序 HelloWorldApp
将只显示问候语 "Hello world!"。要创建此程序,你将:
创建一个源文件
源文件包含用 Java 编程语言编写的代码,你和其他程序员都可以理解。你可以使用任何文本编辑器来创建和编辑源文件。
将源文件编译成.class 文件
Java 编程语言 compiler(javac
)会将你的源文件转换为 Java 虚拟机可以理解的指令。The instructions contained within this .class
file are known as bytecodes.
运行该程序
Java 应用程序 launcher tool(java
)使用 Java 虚拟机来运行你的应用程序。
要创建源文件,你有两个选择:
你可以在计算机上保存文件
,并避免大量输入。Then, you can go straight to Compile the Source File.HelloWorldApp.java
或者,你可以使用以下(较长)的说明。
First, open a shell, or "terminal," window.
A new terminal window.
When you first bring up the prompt, your current directory will usually be your home directory. You can change your current directory to your home directory at any time by typing cd
at the prompt and then pressing Return.
The source files you create should be kept in a separate directory. You can create a directory by using the command mkdir
. For example, to create the directory examples/java
in the /tmp directory, use the following commands:
cd /tmp mkdir examples cd examples mkdir java
To change your current directory to this new directory, you then enter:
cd /tmp/examples/java
Now you can start creating your source file.
Start the Pico editor by typing pico
at the prompt and pressing Return. If the system responds with the message pico: command not found
, then Pico is most likely unavailable. Consult your system administrator for more information, or use another editor.
When you start Pico, it'll display a new, blank buffer. This is the area in which you will type your code.
Type the following code into the new buffer:
/** * The HelloWorldApp class implements an application that * simply prints "Hello World!" to standard output. */ class HelloWorldApp { public static void main(String[] args) { System.out.println("Hello World!"); // Display the string. } }
Be Careful When You Type
javac
)和启动器(java
)都是 case-sensitive,因此你必须一致地大写。HelloWorldApp
与 helloworldapp
不相同。
将代码保存在名为 HelloWorldApp.java
的文件中。In the Pico editor, you do this by typing Ctrl-O and then, at the bottom where you see the prompt File Name to write:
, entering the directory in which you wish to create the file, followed by HelloWorldApp.java
. For example, if you wish to save HelloWorldApp.java
in the directory /tmp/examples/java
, then you type /tmp/examples/java/HelloWorldApp.java
and press Return.
You can type Ctrl-X to exit Pico.
.class
文件Bring up another shell window. 要编译源文件,请将当前目录更改为文件所在的目录。For example, if your source directory is /tmp/examples/java
, type the following command at the prompt and press Return:
cd /tmp/examples/java
If you enter pwd
at the prompt, you should see the current directory, which in this example has been changed to /tmp/examples/java
.
If you enter ls
at the prompt, you should see your file.
Results of the ls
command, showing the .java
source file.
Now are ready to compile the source file. At the prompt, type the following command and press Return.
javac HelloWorldApp.java
编译器生成了一个字节码文件 HelloWorldApp.class
。At the prompt, type ls
to see the new file that was generated: the following figure.
Results of the ls
command, showing the generated .class
file.
既然你有一个 .class
文件,你可以运行你的程序。
如果你在此步骤中遇到问题,请参阅 Common Problems (and Their Solutions)。
In the same directory, enter at the prompt:
java HelloWorldApp
The next figure shows what you should now see.
The output prints "Hello World!" to the screen.
恭喜!你的程序有效!
如果你在此步骤中遇到问题,请参阅 Common Problems (and Their Solutions)。