"Hello world!" for Microsoft Windows

Source: Internet
Author: User

"Hello world!" for Microsoft Windows

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

If you encounter (encounters) problems with the instructions on this page, consult (review) the Common problems (and their Solutions).

    • A Checklist
    • Creating Your First Application
      • Create a Source File
      • Compile the Source file into a .class file
      • Run the program
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-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 (FIT) These instructions if you use a different text editor.

These, need to write your first application.

Creating Your First Application

Your first application, would HelloWorldApp simply display the greeting "Hello world!". To the Create this program, you'll:

    • Create a source file

      A source file contains code, written in the Java programming language, that's and other programmers can understand. You can use any of the 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 T Hat the Java virtual machine can understand. The instructions contained within this file is 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 with the options:

    • You can save the file on HelloWorldApp.java your computer and avoid a lot of typing. Then, you can go straight to Compile the Source file into a .class file.

    • Or, you can use the following (longer) instructions.

First, start your editor. 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!");//Displa     Y the string. }}

Be careful if 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.
HelloWorldAppIs notThe same as helloworldapp.

Save the code in a file with the name HelloWorldApp.java . To does 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 was on the drive myapplication C .
    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 (hint) shows your current directory. When you bring to the prompt, your current directory is usually your home directory for Windows XP (as shown in the preced ing figure.

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

CD C:\myapplication

Now the prompt should C:\myapplication> .

Note:

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

C:\>D:d:\>cd myapplicationd:\myapplication>

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

c:\>cd myapplicationc:\myapplication>dir Volume in drive C are System Volume Serial number is f2e8- c8cc Directory of C:\myapplication2014-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 Freec:\myapplication>

Now is 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 to see the dir new file is generated as follows:

c:\myapplication>javac helloworldapp.javac:\myapplication>dir Volume in drive C-is System Volume Serial number is f2e8-c8cc Directory of C:\myapplication2014-04-24  02:07 PM    <DIR>          . 2014-04-24  02:07 PM    <DIR>          . 2014-04-24  02:07 pm               432 helloworldapp.class2014-04-24  01:34 PM               267 Helloworldapp.java               2 File (s)            699 bytes               2 Dir (s)  93,298,032,640 bytes freec:\myapplication>

Now there .class is a file, you can run the 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 in your screen:

C:\myapplication>JAVA-CP. HelloworldappHello 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.