"Hello World!" for Microsoft Windows

It's time to write your first application! The following instructions are for users of Windows Vista, Windows 7, and Windows 8. Instructions for other platforms are in "Hello World!" for Solaris OS and Linux and "Hello World!" for the NetBeans IDE.

If you encounter(遭遇) problems with the instructions on this page, consult(查阅) the Common Problems (and Their Solutions).


A Checklist 

To write your first program, you'll need:

  1. The Java SE Development Kit 8 (JDK 8)

    You can download the Windows version now. (Make sure you download the JDK, not the JRE.) Consult the installation instructions.

  2. A text editor

    In this example, we'll use Notepad, a simple editor included with the Windows platforms. You can easily adapt(适应) these instructions if you use a different text editor.

These two items are all you'll 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 code, 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 programming language compiler (javac) 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 application launcher tool (java) uses the Java virtual machine to run your application.

Create a Source File

To create a source file, you have two options:

First, start your editor. You can launch the Notepad editor from the Start menu by selecting Programs > Accessories > Notepad. In a new document, type in the following code:

/**  * 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


Note: Type all code, commands, and file names exactly as shown. Both the compiler (javac) and launcher (java) are case-sensitive, so you must capitalize consistently.

HelloWorldApp is not the same as helloworldapp.


Save the code in a file with the name HelloWorldApp.java. To do this in Notepad, first choose the File > Save As menu item. Then, in the Save As dialog box:

  1. Using the Save in combo box, specify the folder (directory) where you'll save your file. In this example, the directory is myapplication on the C drive.
  2. In the File name text field, type "HelloWorldApp.java", including the quotation marks.
  3. From the Save as type combo box, choose Text Documents (*.txt).
  4. In the Encoding combo box, leave the encoding as ANSI.

When you're finished, the dialog box should look like this.

The Save As dialog just before you click Save.

Now click Save, and exit Notepad.

Compile the Source File into a .class File

Bring up a shell, or "command," window. You can do this from the Start menu by choosing Run... and then entering cmd. The shell window should look similar to the following figure.

A shell window.

The prompt(提示) shows your current directory. When you bring up the prompt, your current directory is usually your home directory for Windows XP (as shown in the preceding figure.

To compile your source file, change your current directory to the directory where your file is located. For example, if your source directory is myapplication on the C drive, type the following command at the prompt and press Enter:

cd C:\myapplication

Now the prompt should change to C:\myapplication>.


Note:

To change to a directory on a different drive, you must type an extra command: the name of the drive. For example, to change to the myapplication directory on the D drive, you must enter D:, as follows:

C:\>D:

D:\>cd myapplication

D:\myapplication>

If you enter dir at the prompt, you should see your source file, as follows:

C:\>cd myapplication

C:\myapplication>dir
Volume in drive C is System
Volume Serial Number is F2E8-C8CC Directory of C:\myapplication 2014-04-24 01:34 PM <DIR> .
2014-04-24 01:34 PM <DIR> ..
2014-04-24 01:34 PM 267 HelloWorldApp.java
1 File(s) 267 bytes
2 Dir(s) 93,297,991,680 bytes free C:\myapplication>

Now you are ready to compile. At the prompt, type the following command and press Enter.

javac HelloWorldApp.java

The compiler has generated a bytecode file, HelloWorldApp.class. At the prompt, type dir to see the new file that was generated as follows:

C:\myapplication>javac HelloWorldApp.java

C:\myapplication>dir
Volume in drive C is System
Volume Serial Number is F2E8-C8CC Directory of C:\myapplication 2014-04-24 02:07 PM <DIR> .
2014-04-24 02:07 PM <DIR> ..
2014-04-24 02:07 PM 432 HelloWorldApp.class
2014-04-24 01:34 PM 267 HelloWorldApp.java
2 File(s) 699 bytes
2 Dir(s) 93,298,032,640 bytes free C:\myapplication>

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

If you encounter problems with the instructions in this step, consult the Common Problems (and Their Solutions).

Run the Program

In the same directory, enter the following command at the prompt:

java -cp . HelloWorldApp

You should see the following on your screen:

C:\myapplication>java -cp . HelloWorldApp
Hello World! C:\myapplication>

Congratulations! Your program works!

If you encounter problems with the instructions in this step, consult the Common Problems (and Their Solutions).

"Hello World!" for Microsoft Windows的更多相关文章

  1. Microsoft Windows* SDK May 2010 或较新版本(兼容 2010 年 6 月 DirectX SDK)GPU Detect

    原文链接 下载代码样本 特性/描述 日期: 2016 年 5 月 5 日 GPU Detect 是一种简短的示例,演示了检测系统中主要显卡硬件(包括第六代智能英特尔® 酷睿™ 处理器产品家族)的方式. ...

  2. 需要正确安装 Microsoft.Windows.ShellExperienceHost 和 "Microsoft.Windows.Cortana" 应用程序。

    windows 10 开始菜单修复工具 Win10开始菜单修复工具出现的原因,自从升级到Windows  10,一直BUG不断,而其中有一个BUG非常的让你印象深刻,就是开始菜单无响应,你用着用着电脑 ...

  3. 驱动开发利器Microsoft Windows Driver Kit 7.1.0下载

    在Windows 2000 与Windows XP 系统采用是WINDDK来开发WINDOWS驱动程序,我手头也有WINDDK,可是从Windows Vista开始之后,一般采用Microsoft W ...

  4. C++ 无法将值写入注册表 HKEY_LOCAL_MACHINE\Sofeware\Microsoft\Windows\CurrentVersion\Run,以实现开机自启动应用程序。

    C++  无法将值写入注册表 HKEY_LOCAL_MACHINE\Sofeware\Microsoft\Windows\CurrentVersion\Run,以实现开机自启动应用程序.但可以写入到H ...

  5. [Solution] Microsoft Windows 服务(1) C#创建Windows服务

    Microsoft Windows 服务(即,以前的 NT 服务)使您能够创建在它们自己的 Windows 会话中可长时间运行的可执行应用程序.这些服务可以在计算机启动时自动启动,可以暂停和重新启动而 ...

  6. Hyperion Business Modeling for Microsoft Windows (32-bit)

    介质包搜索 常见问题    说明   复查 许可证列表 以确定需要下载的产品程序包. 请选择产品程序包和平台,然后单击“查找”. 如果只有一项结果,则可以看到下载页.如果有多个结果,请选择一个,然后单 ...

  7. Oracle Fusion Applications (11.1.8) Media Pack and Oracle Application Development Framework 11g (11.1.1.7.2) for Microsoft Windows x64 (64-bit)

    Oracle Fusion Applications (11.1.8) Media Pack for Microsoft Windows x64 (64-bit) 重新搜索   常见问题    提示  ...

  8. Microsoft Windows 远程权限提升漏洞(CVE-2013-3175)(MS13-062)

    漏洞版本: Microsoft Windows XP Microsoft Windows Vista Microsoft Windows Server 2008 Microsoft Windows R ...

  9. Microsoft Windows Sharepoint Services V3.0 安装图示

    本文以图示的方式,向读者展示Microsoft Windows SharePoint Services V3.0的安装过程. 在以下图示的安装过程中,使用了下面所列出的软件: Windows Serv ...

随机推荐

  1. 如何用django开发一个简易个人Blog

    功能概要:(目前已实现功能) 公共展示部分: 1.网站首页展示已发布的博客记录,包括名称.摘要信息.发布日期.阅读量及评论数. 2.首页文章列表可按照分类筛选. 3.点击标题或阅读全文链接,进入博客阅 ...

  2. 可拖动的DIV续

    之前写过一篇可拖动的DIV讲如何实现可拖动的元素,最后提出了几点不足,这篇文章主要就是回答着三个问题 1. 浏览器兼容性 2. 边界检查 3. 拖动卡顿.失灵 先附上上次代码 <!DOCTYPE ...

  3. Effective c#学习笔记(1)

    条款1 使用属性代替可访问的数据成员 属性是个啥,谁在用? 1. C#的属性在被访问的时候看起来好像是数据成员,但其实是方法.     2. 在.NET框架中,一般使用属性表达公有数据成员.      ...

  4. DDD~领域事件中使用分布式事务

    回到目录 对于一个聚合来说,它可能会被附加很多事件,这里我们叫它领域事务,因为一个聚会我们可以把它理解成一个领域,一个业务.对于领域事件不清楚的同学可以看看我的这篇文章<DDD~领域事件与事件总 ...

  5. EF架构~为BulkInsert引入SET IDENTITY_INSERT ON功能

    回到目录 在设计表结构时,我们通常将不是很在乎表现的主键设计成自增长的,大数据量用bigint,一般地用int就可以了,int就是C#里的Int32,它最大可以存储到2147483647,21亿,基本 ...

  6. Dapper sql in

    应用场景: 使用 sql的 Case When Then 批量更新某张表,底层数据库用到了Dapper 代码示例: public int UpdateClientReceivedResult(Dict ...

  7. Android后台保活实践总结:即时通讯应用无法根治的“顽疾”

    前言 Android进程和Service的保活,是困扰Android开发人员的一大顽疾.因涉及到省电和内存管理策略,各厂商基于自家的理解,在自已ROOM发布于都对标准Android发行版作为或多或少的 ...

  8. SQL Server 2012 T-SQL 新特性

    序列 Sequence SQL Server 现在将序列当成一个对象来实现,创建一个序列的例子语法如下: CREATE SEQUENCE DemoSequence START WITH 1 INCRE ...

  9. OpenCascade Tcl vs. ACIS Scheme

    OpenCascade Tcl vs. ACIS Scheme eryar@163.com 摘要Abstract:本文通过OpenCascade的Tcl/Tk和ACIS的Scheme的对比来说明脚本语 ...

  10. .NET面试题解析(07)-多线程编程与线程同步

      系列文章目录地址: .NET面试题解析(00)-开篇来谈谈面试 & 系列文章索引 关于线程的知识点其实是很多的,比如多线程编程.线程上下文.异步编程.线程同步构造.GUI的跨线程访问等等, ...