You are on page 1of 18
Using NetBeans™ to Compile and Run Java Programs This document is based on NetBeans 4.1 and 5.5. (The document has two sections. The first section covers NetBeans 5.5; the second section covers NetBeans 4.1. But really, the instructions for NetBeans 4.1 and NetBeans 5.5 are almost identical.) For information on other versions of NetBeans, contact the author via email: BeginProg2 at BurdBrain dot com. NetBeans 5.5 1. Download NetBeans by visiting http://www.netbeans.org. 2. Install the downloaded NetBeans program on your computer. 3. Run NetBeans by clicking the NetBeans icon. ‘You see the NetBeans 5.5 opening screen. opm celtvny ame Soma ha ow onstastor 4, Click Create New Project. ‘The New Project dialog appears. Grete spe coos rosea 2 (Choose Project GB trerree 1D messin Pucin races © Sorte Destin oes (eva poseaton Javad ubray @ drama tneesingsecos Br serapaoc utneasang ac samt ‘creates anew 1258 application nastaidSIDE pice. Youcan abo senaate aman dash theprows. slandacrojects sean IDE generated Ant bald script fo Bly rary ane debug su oro 5. In the Categories pane, select General, and in the Projects pane, select Java Application, 6. Click Next. ‘The New Java Application dialog appears. Now Java Application Name and Location Propet: [Snatgpeatnt rome tecton: [EBS Pree Feder: [Rpburivawoslesient IF ce eT] 7. Unless you're fussy, you can accept whatever defaults you see in the Project Name, Project Location, and Project Folder fields. Leave Set as Main Project checked. But in this example, you should uncheck Create Main Class. 8. Click Finish. A new project tree appears in the NetBeans Projects window 1 NetBeans IDE 5.5 - Javahpplicationt Fle Edt View WNavigate Source Refactor Bull BRBe¢tasr, “Projects x | Files [Runtime e- & Eee ‘B-C Source Packages (@-GB Test Packages eG Uraes eC Test Loraes 9. On the project tree, right-click the Source Package branch. In the resulting context menus, select New —> Java Class. On a Macintosh, substitute Ctrl-Click for right-click, Cree Fie Edt Ven Nevgste Souce Referor Buld Run CVS Toos Whdow Hep Gi GB Test Packages 5G Unaies | 2 Teli | | © dava package | (a) tevarrterace | [Panel Form. | ADremetem | 1B entry class, B Entty Classes From Database. Web Service Chet ‘The New Java Class dialog appears. Sr steps ‘Name and vocation 2 choose re Te casshane! MyRistavatins 2 Name and Location rome: avarecesiont Leste [ieee Plager cosedele: [idbudoavareplatonlne rrsosradsiaa ver: As 1.4, shy recmmenceth yeu do NOT place 3a csssin the es, 10. In the Class Name field, type the name of the class that you want to create, In this example, I type MyFirstJavaClass, because I intend to run the code in Listing 3-1. The name of the Java source file is MyFirstJavaClass.. java, but in the Class Name field, I type MyFirstJavaClass, without the . java extension, 11. Click Finish. The new class appears in the Projects window. Some skeletal code for the new class appears in the editor. EiNsoer 95.5. matppiaton Gees) ie ta ven hase Savee feat Bas fan CS Toss Won Beer 9/2993) 5% SF eBloo ee mee @ [2 8) 8 sl 12. If you’re creating code from scratch, you can type additional code in the editor. But if you’re running an example from the book, copy that example’s code, and paste the code into the NetBeans editor (replacing the skeletal code that’s already in the editor). Fre ES Uw Hagste Sasce felarar Balt Pin CUS Tose Undo Hes BBEMYEBNFP ABBOWYdDD Projects > es | runine 2 Javanppleation! Sg Sauce Patan © etna pasaoes Vike] Ep Wifes se) BB mrstinacas ve a 2G tecredages saved on May 16, Leas 63 tas 2 Ge Teelber to change this template, 3 void muin(sceiog e921) ( cuespeantaa "cnse9Laee, reyaltses, a1eep"): 13. On the main NetBeans menu, choose File — Save. 14. In the NetBeans Projects window, right-click the current project branch. In the resulting context menu, select Set Main Project. On a Macintosh, substitute Ctrl-Click for right-click, Seat ie EA Ven Nevgste source Refactor @uld Run CVS. Took Robe eBENeD AK untime || Wekone_ >] 5G} Source Package BG Build Main Project. The output window shows a successful compilation, Cpr vege Gi) inet S If the compilation isn’t successful, make changes to the code in the editor window, and try compiling again, 16. In the Projects window, right-click the current project’s branch, In the resulting context menu, select Properties. (On a Macintosh, substitute Ctrl-Click for right-click.) The Project Properties dialog appears, eee sees sorcetaow Fos: oon oon rome rce i buddavatoretint cae = ewes Pacioges Test aan Firs Fade Folin sarceterd: (16 17. On the left side of the Project Properties dialog, select Run. ‘The fields on the right side change to include Main Class, Arguments, and so on. © sacs ancins fered e° aw a _t 2 Comins Ce | | Docanrths Gee Ga seaIoeT 18. In the Main Class field, type the name of the class that contains the main method. In this example, I type MyFirstJavaClass, because I intend to run the code in Listing 3-1. The name of the Java source file is MyFirstJavaClass. java, but in the Main Class field, Itype MyFirstJavaClass, without the . java extension. 19. Click OK to dismiss the Project Properties dialog. 20. On the main NetBeans menu, choose Run —> Run Main Project. ‘The program's output appears in the NetBeans Output window, and everybody is happy. Chocolate, royalties, sleep EUILD SUCCESSFUL (total tine: 1 seconai| Fished builing JevaAppication! (run) NetBeans 4.1 1. Download NetBeans by visiting http://www.netbeans.org. 2. Install the downloaded NetBeans program on your computer. 3. Run NetBeans by clicking the NetBeans icon. ‘You see the NetBeans 4.1 opening screen. {Ble EG Veew Bold Hun Relear Vewioning Tool Window Help Bee reaarce = © — &) netBeaiisipEs; sin a = Sa 4. Click New Project. The New Project dialog appears. 10 steps ose jet 1. choose rot co sons z Scones 2 er 5 eachertrr Si ttepee otros th sha Sares es Ivete thcaseg at ont coat Creates anew 2 appheatonnostard 0 pon, you ca akoamaate manish Uoprowa: Sado on bE-gearted Nob segs bly snag sara 5. In the Categories pane, select General, and in the Projects pane, select Java, Application, 6. Click Next. ‘The New Java Application dialog appears iL (Now Java Application a Steps Nome and Location 2 chose rojct, Popstar: xatelcaton 2. Name and Lveetion Propeocatens EinuPrenane Ge) PrsFader: (CinaPtonareasbprlctint ise asna poke: Dieses nancias: 7. Unless you’re fussy, you can accept whatever defaults you see in the Project Name, Project Location, and Project Folder fields. Leave Set as Main Project checked. But in this example, you should uncheck Create Main Class. 8. Click Finish. A new project tree appears in the NetBeans Projects window. NetBeans IDE 4.1 JavaApplicati Build Run Ref [iProj.. 4x ‘Files Runtime & [avanpplicationt B- SoucePackages 2G Tes: Peckages PB Libraries (GB Tes Libraries 12 9. On the project tree, right-click the Source Package branch. In the resulting context menus, select New — Java Class. On a Macintosh, substitute Ctrl-Click for right-click. ing Tools Window fOEBFP BAO Gq Test Pactages > > FllerFoter... Pip Tettices | Finds Cie |B Uaval Glas BEE © Java Packaye.. Java Interface. Tools Janet Form. Properties ‘TJFrame Form... ‘The New Java Class dialog appears. ‘Biew Java Class a seeps ame. ond tocotan fceeetrierm Choe eT Ge ties Hyfitrane owe; [Jevoporeatont ca OO Gesedhie [ChisaPogarcimsipeiatenjechvretnetherjws Yeni: of DK Lhd rerarended ha youd NOT pe Jeeclar nthe def pckage 3 10. In the Class Name field, type the name of the class that you want to create. In this example, I type MyFirstJavaClass, because I intend to run the code in Listing 3-1. The name of the Java source file is MyFirstJavaClass.. java, but in the Class Name field, I type MyFirstJavaClass, without the . java extension, 11. Click Finish, The new class appears in the Projects window. Some skeletal code for the new class appears in the editor. joss EU inanggieniea isi} ‘iow lla Ran Reesor Versioning Tools Window Help bea +a YGF ABSRDD a eo 5 Sa eo POSE Eee fujealee 2 chad pains i ‘ ra 12. If you’re creating code from scratch, you can type additional code in the editor. But if you’re running an example from the book, copy that example’s code, and paste the code into the NetBeans editor (replacing the skeletal code that’s already in the editor). 14 (pWeiBeane WEAN Jovarppiicaint lo Edit Viow Build Bun Rofacor Vouioning Toole Window Help BGEBB+EBAFP BBODD Fok < x es [-Runtne | Wane x (URIs >) SS Seaton $9 (FAP BLP AS HHloo GS sare ets Anse KyPisattevatiass © EE ¥ " ‘ Bynes et i bf Tat Pade method asin 9G leas : AB Teter: : puonie static woud raun(sezmnet ozs) ( ) > 13. On the main NetBeans menu, choose File > Save. k the current project branch. In the 14, In the NetBeans Projects window, right- resulting context menu, select Set Main Project. On a Macintosh, substitute Ctrl-Click for right-click. 15, NetBeans IDF #1” JavaApplicationt ‘TeveApplcationd (@ Source Pecke New re Build Project 2G TetPackege Clan and Build Project BG Libres | Clean Project (GB Tet Lbvaies Generate Javadoc for Project | Run Project Debug Project Set Main Project out Open Required Proj Close Project Find Cirle Navigator —_ Properties 15. On the main NetBeans menu, choose Build —> Build Main Project. ‘The output window shows a successful compilation, |v God zs Be ce andro «J Figen ites If the compilation isn’t successful, make changes to the code in the editor window, and try compiling again. 16. In the Projects window, right-click the current project’s branch. In the resulting context menu, select Properties. (On a Macintosh, substitute Ctrl-Click for right-click.) The Project Properties dialog appears. 16 Prom bidet claavahoaneaaAkatns sucersdian Fidos (rt eer ate } = ewes aces | rrtog ir ote ] tet lessons 17. On the left side of the Project Properties dialog, select Run. The fields on the right side change to include Main Class, Arguments, and so on. 7 Praja Drop nien evap TenonT a =a 9 tence tytn — ss Moti eo ‘Documenting ee rama (esta 18. In the Main Class field, type the name of the class that contains the main method. In this example, I type MyFirstJavaClass, because I intend to run the code in Listing 3-1. The name of the Java source file is MyF ir st JavaClass.. java, but in the Main Class field, I type MyFirstJavaClass, without the . java extension. 19. Click OK to dismiss the Project Properties dialog. 20. On the main NetBeans menu, choose Run —> Run Main Project. ‘The program’s output appears in the NetBeans Output window, and everybody is happy. ‘Output - JavaApplicationt (eun) ceps~sar: compile Chocolate, royalties, sleep EUILD SUCCESSFUL (vocal time: 0 seconss|| 18,

You might also like