1 Development Environments AUBG, COS dept Lecture Title: Dev Env: NetBeans (Extract from Syllabus) Reference:
2 Lecture Contents: §NetBeans – introduction §NetBeans – functionality l NetBeans IDE Java Quick Start Tutorial ( l Introduction to GUI Building ( functionality.html) §NetBeans – configuration
3 NetBeans – Introduction §NetBeans refers to both l NetBeans Platform l NetBeans IDE §NetBeans Platform is a reusable framework for simplifying the development of Java Swing desktop applications. §NetBeans IDE is an open-source integrated development environment that supports development of all Java application types.
4 NetBeans – introduction §Currently installed NetBeans IDE version §Develop desktop, mobile and web applications with Java, PHP, C/C++ and more. §Runs on Windows, Linux, Mac OS X and Solaris.
5 NetBeans – functionality NetBeans IDE Java Quick Start §Setting up the project l To Create an IDE project §Adding Code to the Generated Source File §Compiling and running a project §Building and deploying the application (.jar file) §Create javadoc §Packaging and Distributing Java Desktop Applications §Tips for NetBeans users
6 Setting Up the Project §.§.
7 To create an IDE project: §1. Start NetBeans IDE. §2. In the IDE, choose File > New Project… (Ctrl+Shift+N), as shown in the figure below.
8 To create an IDE project: §3. In the New Project wizard, expand the Java category and select Java Application as shown in the figure below. Then click Next.
9 To create an IDE project: §4. In the Name and Location page of the wizard, do the following (as shown in the figure above): l In the Project Name field, type on your choice. l Leave the Use Dedicated Folder for Storing Libraries checkbox unselected. l In the Create Main Class field, type. on your choice (if it is empty). l Leave the Set as Main Project checkbox selected.
10 To create an IDE project: §5. Click Finish. The project is created and opened in the IDE. You should see the following components: l The Projects window, which contains a tree view of the components of the project, including source files, libraries that your code depends on, and so on. l The Source Editor window with a file called.java open. l The Navigator window, which you can use to quickly navigate between elements within the selected class. l The Tasks window, which lists compilation errors as well other tasks that are marked with keywords.
11 Adding Code to the Generated Source File §Because you have left the Create Main Class checkbox selected in the New Project wizard, the IDE has created a skeleton main class for you. You can add the "Hello World!" message to the skeleton code by replacing the line: // TODO code application logic here with the line: System.out.println("Hello World!"); §Save the change by choosing File > Save. §The file should look something like the following code sample.
12 Adding Code to the Generated Source File /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package helloworldapp; /** * */ public class HelloWorldApp { /** args the command line arguments */ public static void main(String[] args) { System.out.println("Hello World!"); }
13 Compiling and running a project §Because of the IDE's Compile on Save feature, you do not have to manually compile your project in order to run it in the IDE. When you save a Java source file, the IDE automatically compiles it. (how to switch this option off, see Project > Properties > Compiling). §To run the program: l Choose Run > Run Main Project (F6). l The next figure shows what you should now see. l Congratulations! Your program works.
14 Compiling and running a project
15 Compiling and running a project §If there are compilation errors, they are marked with red glyphs in the left and right margins of the Source Editor. l The glyphs in the left margin indicate errors for the corresponding lines. l The glyphs in the right margin show all of the areas of the file that have errors, including errors in lines that are not visible. §You can mouse over an error mark to get a description of the error. §You can click a glyph in the right margin to jump to the line with the error.
16 Compiling and running a project §Here are more options to process the project: §To test the program: l Choose Run > Test Project( ) (Alt+F6) l Try the command to know what you should now see. l Open the Output window (if it is closed) and have a look at the report generated in it.
17 Compiling and running a project §Here are more options to process the project: §To build only (without run) the program: l Choose Run > Build Main Project (F11) l Try the command to know what you should now see. l Open the Output window (if it is closed) and have a look at the report generated in it.
18 Compiling and running a project §Here are more options to process the project: §To duplicate the recent command given: l Choose Run > Repeat Build/Run: (jar) l Try the command to know what you should now see. l Open the Output window (if it is closed) and have a look at the report generated in it.
19 Compiling and running a project §Here are more options to process the project: §To establish project settings: l Click right mouse btn on Project name, select Properties OR l Choose Run > Set project configuration > Customize… l Try the command to know what you should now see. l Project Properties screen gets opened – you are free to manipulate/establish/modify current project parameters.
20 Compiling and running a project §Here are more options to process the project: §To establish Main project settings: l Click right mouse btn Project name, select Set as Main project OR l Choose Run > Set Main project > None Select among all projects for the current NetBeans session l Try the command to know what you should now see. l List of existing projects gets displayed and you are to select/set the main project for the current session.
21 Compiling and running a project §Here are more options to process the project: §To create archive (.jar) file for the program: l Choose Run > Clean and Build Main Project (Shift+F11) l Try the command to know what you should now see. l Open the Output window (if it is closed) and have a look at the report generated in it. l For details see next slide.
22 Building and deploying the application (.jar file) §Once you have written and test run your application, you can use the Clean and Build command to build your application for deployment. When you use the Clean and Build command, the IDE runs a build script that performs the following tasks: l Deletes any previously compiled files and other build outputs. l Recompiles the application and builds a JAR file containing the compiled files. §To build your application: l Choose Run > Clean and Build Main Project (Shift+F11) §You can view the build outputs by opening the Files window and expanding the node. The compiled bytecode file.class is within the build/classes/ subnode. A deployable JAR file that contains the.class is within the dist node.
23 Building and deploying the application (.jar file)..
24 Create javadoc §To be discussed in separate lecture on SW Documentation.
25 Packaging and Distributing Java Desktop Applications One question that a lot of beginning programmers have is: "Now that I've created my application in the IDE, how do I get it to work from the command line outside of the IDE." Similarly, someone might ask, "How do I distribute this application to other users without having to give them the whole IDE as well?" The answers to these questions are relatively simple, but not necessarily obvious. This document addresses those questions by taking you through the basics of using the IDE to prepare your applications for distribution and deployment. In addition, this document provides information that you might need to configure your system (or which you might need to pass on to the users of your application). We will show a few different approaches for deploying an application, so that users can access the application by: Double-clicking the application's Java Archive (JAR) file. Calling the application from the command line. Calling the application from a script file. Using Java Web Start
26 Tips for NetBeans users §The Compile on Save feature can be turned off in the Project Properties window. Right-click your project, select Properties. In the Properties window, choose the Compiling tab. The Compile on Save checkbox is right at the top. §Note that in the Project Properties window you can configure numerous settings for your project: project libraries, packaging, building, running, etc.
27 NetBeans – configuration.
28 Exercises/Tasks §Write a Java program to test the options for the main menu Run command: §To display 5 times the string “Good luck, dear student!!” §To enter two numeric integer/real values and To display their sum and product.
29 Exercises/Tasks for(int i=1; i<=5; i++) { System.out.print("Good luck, dear"); System.out.println(" student!!"); }
30 Exercises/Tasks import java.util.Scanner; … Scanner cin = new Scanner(System.in); System.out.println("\n Enter two real numeric values:"); double inp1, inp2, result1; inp1 = cin.nextDouble(); inp2 = cin.nextDouble(); result1 = inp1 + inp2; System.out.println(" result of addition is = " + result1);
31 Exercises/Tasks import java.util.Scanner; public class INF160Proba3 { /** args the command line arguments */ public static void main(String[] args) { for(int i=1; i<=5; i++) System.out.println("Good luck, dear student!!"); Scanner cin = new Scanner(System.in); System.out.println("\n Enter two real numeric values:"); double inp1, inp2, result1; inp1 = cin.nextDouble(); inp2 = cin.nextDouble(); result1 = inp1 + inp2; System.out.println(" result of addition is = " + result1); } // end of main } // end of class
32 Exercises/Tasks §Write a Java program to test the command-line arguments option §How to specify arguments l Right-click your project, select Properties. In the Properties window, choose the Run tab. Enter arguments §How to access arguments public static void main(String[] args) { for (int i=0; i<args.length; i++) System.out.println(args[i]); }
33 Exercises/Tasks §How to access arguments, second more reliable version public static void main(String[] args) { System.out.println("\n\n Command line arguments processed"); if (args.length >0) { for(int i=0; i<args.length; i++) System.out.println(args[i]); } else System.out.println(" No cmd line arguments specified"); }
34 Exercises/Tasks §Write a Java program to test the command-line arguments option §Run from within NetBeans §Run as a separate OS command java –jar Application.rar
35 Exercises/Tasks §Write a Java program to test the main menu Refactor command. §Rename variables l inp1 into input1 l inp2 into input2 l result1 into res1
36 Exercises/Tasks §Write a Java program to test the §GCD problem §Fibonacci problem §Factoriel problem
37 NetBeans – functionality Introduction to GUI Building §Contents § §Exercise 1: Creating a Project §Exercise 2: Building the Front End §Exercise 3: Adding Functionality §Exercise 4: Running the Program §How Event Handling Works
38 Thank You For Your Attention!