creating your first application

Upload: john-paul-cordova

Post on 02-Mar-2018

222 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/26/2019 Creating Your First Application

    1/13

    Creating Your First Application

    Your first application, HelloWorldApp, will simply display the greeting "Hello World!" To create this program,

    you will:

    Create an IDE project

    When you create an IDE proect, you create an enironment in which to uild and run your

    applications# $sing IDE proects eliminates configuration issues normally associated with deeloping

    on the command line# You can uild or run your application y choosing a single menu item within the

    IDE#

    Add code to the generated source file

    % source file contains code, written in the &aa programming language, that you and other

    programmers can understand# %s part of creating an IDE proect, a s'eleton source file will e

    automatically generated# You will then modify the source file to add the "Hello World!" message#

    Compile the source file into a .class file

    The IDE ino'es the &aa programming language compiler(javac), which ta'es your source file

    and translates its te(t into instructions that the &aa irtual machine can understand# The instructions

    contained within this file are 'nown as bytecodes#

    Run the program

    The IDE ino'es the &aa application launcher tool)java*, which uses the &aa irtual machine to run

    your application#

    Create an IDE Project

    To create an IDE proect:

    +# aunch the -et.eans IDE#

    o /n 0icrosoft Windows systems, you can use the -et.eans IDE item in the 1tart menu#

    o /n 1olaris /1 and inu( systems, you e(ecute the IDE launcher script y naigating to the

    IDE2s bindirectory and typing ./netbeans.

    o /n 0ac /1 3 systems, clic' the -et.eans IDE application icon#

    4# In the -et.eans IDE, choose File5 New Project...#

  • 7/26/2019 Creating Your First Application

    2/13

    -et.eans IDE with the 6ile 5 -ew 7roect menu item selected#

    8# In the New Projectwi9ard, e(pand the a!acategory and select a!a Applicationas shown in the

    following figure:

  • 7/26/2019 Creating Your First Application

    3/13

    -et.eans IDE, -ew 7roect wi9ard, hoose 7roect page#

    ;# In the Name and "ocationpage of the wi9ard, do the following )as shown in the figure elow*:

    o In the Project Namefield, type Hello World App#

    o In the Create #ain Classfield, type helloworldapp.HelloWorldApp#

  • 7/26/2019 Creating Your First Application

    4/13

    -et.eans IDE, -ew 7roect wi9ard, -ame and ocation page#

  • 7/26/2019 Creating Your First Application

    5/13

    -et.eans IDE with the HelloWorld%pp proect open#

    Add D% & to the Platform "ist 'if necessar()

    It may e necessary to add &D> ? to the IDE2s list of aailale platforms# To do this, choose Tools 5 &aa

    7latforms as shown in the following figure:

  • 7/26/2019 Creating Your First Application

    6/13

    1electing the &aa 7latform 0anager from the Tools 0enu

    If you don2t see &D> ? )which might appear as +#? or +#?#@* in the list of installed platforms, clic' Add Platform,

    naigate to your &D> ? install directory, and clic' Finish# You should now see this newly added platform:

  • 7/26/2019 Creating Your First Application

    7/13

    The &aa 7latform 0anager

    To set this &D> as the default for all proects, you can run the IDE with the --jdkhomeswitch on the

    command line, or y entering the path to the &D> in the netbeans_j2sdkhomeproperty of

    yourINSTALLATION_DIRECTORY/etc/netbeans.conf file#

    To specify this &D> for the current proect only, select *ello +orld Appin the Projectspane,

    choose File5 Project Properties '*ello +orld App), clic' "i,raries, then select D% -.&in the a!a

    Platformpulldown menu# You should see a screen similar to the following:

  • 7/26/2019 Creating Your First Application

    8/13

    The IDE is now configured for &D> ?#

    Add Code to the enerated $ource File

    When you created this proect, you left the Create #ain Classchec'o( selected in the New Projectwi9ard#

    The IDE has therefore created a s'eleton class for you# You can add the "Hello World!" message to the

    s'eleton code y replacing the line:

    // TODO code application logic here

  • 7/26/2019 Creating Your First Application

    9/13

    with the line:

    System.out.println("Hello World!"); // Display the string.

    /ptionally, you can replace these four lines of generated code:

    /

    author

    /

    with these lines:

    /

    The HelloWorldpp class implements an application that

    simply prints "Hello World!" to standard output.

    /

    These four lines are a code comment and do not affect how the program runs# ater sections of this tutorial

    e(plain the use and format of code comments#

    /e Careful +hen You 0(pe

    Note1Type all code, commands, and file names e(actly as shown# .oth the compiler )javac* and launcher

    )java* are case-sensitive, so you must capitali9e consistently#

    HelloWorldAppis notthe same as helloworldapp#

    1ae your changes y choosing File5 $a!e#

    The file should loo' something li'e the following:

    /

    To change this template# choose Tools $ Templates

    and open the template in the editor.

    /

    pac%age hello&orldapp;

    /

    The HelloWorldpp class implements an application that

    simply prints "Hello World!" to standard output.

    /

    pu'lic class HelloWorldpp

    /

  • 7/26/2019 Creating Your First Application

    10/13

    param args the command line arguments

    /

    pu'lic static oid main(String*+ args)

    System.out.println("Hello World!"); // Display the string.

    ,

    ,

    Compile the $ource File into a .class File

    To compile your source file, choose Run5 /uild Project '*ello +orld App)from the IDE2s main menu#

    The /utput window opens and displays output similar to what you see in the following figure:

  • 7/26/2019 Creating Your First Application

    11/13

    /utput window showing results of uilding the HelloWorld proect#

    If the uild output concludes with the statement BUILD SUCCESSFUL, congratulations! You hae successfully

    compiled your program!

    If the uild output concludes with the statement BUILD FAILED, you proaly hae a synta( error in your

    code# Errors are reported in the /utput window as hyperlin'ed te(t# You douleAclic' such a hyperlin' to

    naigate to the source of an error# You can then fi( the error and once again choose Run5 /uild Project#

  • 7/26/2019 Creating Your First Application

    12/13

    When you uild the proect, the ytecode file HelloWorldApp.classis generated# You can see where the

    new file is generated y opening the Fileswindow and e(panding

    the *ello +orld App2,uild2classes2helloworldappnode as shown in the following figure#

    6iles window, showing the generated #class file#

    -ow that you hae uilt the proect, you can run your program#

    Run the Program

    6rom the IDE2s menu ar, choose Run5 Run #ain Project#

    The ne(t figure shows what you should now see#

    The program prints "Hello World!" to the /utput window )along with other output from the uild script*#

    ongratulations! Your program wor's!

    Continuing the 0utorial with the Net/eans IDE

  • 7/26/2019 Creating Your First Application

    13/13

    The ne(t few pages of the tutorial will e(plain the code in this simple application# %fter that, the lessons go

    deeper into core language features and proide many more e(amples# %lthough the rest of the tutorial does not

    gie specific instructions aout using the -et.eans IDE, you can easily use the IDE to write and run the sample

    code# The following are some tips on using the IDE and e(planations of some IDE ehaior that you are li'ely

    to see:

    /nce you hae created a proect in the IDE, you can add files to the proect using the New Filewi9ard#

    hoose File5 New File, and then select a template in the wi9ard, such as the Empty &aa 6ile

    template#

    You can compile and run an indiidual file )as opposed to a whole proect* using the IDE2s Compile

    File)6B* and Run File)1hiftA6C* commands# If you use the Run #ain Projectcommand, the IDE will

    run the file that the IDE associates as the main class of the main proect# Therefore, if you create an

    additional class in your HelloWorld%pp proect and then try to run that file with the Run #ain

    Projectcommand, the IDE will run theHelloWorldAppfile instead#

    You might want to create separate IDE proects for sample applications that include more than one

    source file#

    %s you are typing in the IDE, a code completion o( might periodically appear# You can either ignore

    the code completion o( and 'eep typing, or you can select one of the suggested e(pressions# If you

    would prefer not to hae the code completion o( automatically appear, you can turn off the feature#

    hoose 0ools5 3ptions5 Editor, clic' the Code Completionta and clear the Auto Popup

    Completion +indowchec'o(#

    If you want to rename the node for a source file in the Projectswindow, choose Refactorfrom IDE2s

    main menu# The IDE prompts you with the Renamedialog o( to lead you through the options ofrenaming the class and the updating of code that refers to that class# 0a'e the changes and

    clic' Refactorto apply the changes# This se=uence of clic's might seem unnecessary if you hae ust

    a single class in your proect, ut it is ery useful when your changes affect other parts of your code in

    larger proects#

    6or a more thorough guide to the features of the -et.eans IDE, see the-et.eans

    Documentationpage#

    https://netbeans.org/kb/https://netbeans.org/kb/https://netbeans.org/kb/https://netbeans.org/kb/https://netbeans.org/kb/