Next page | Contents page |

Exercise 16 - starting NetBeans

  1. Download and install the current version of the free Integrated Development Environment NetBeans, from www.netbeans.org
    (The following steps were written using NetBeans version 6.8)
  2. On the File menu select New project...
  3. In the New project dialogue select Java as the category and Java application as the project. Click the Next button.
  4. In the next page of the dialogue type Ex16 as the project name , set a suitable project location (the last part of the path ought to be ex16). Of the check boxes, only tick "Set as main project" for now (you can try other possibilities yourself later). Click the Finish button.
  5. In the top left window of NetBeans ensure the Projects tab is selected. You should now see your new project, Ex16. Open up the + boxes to see it something like this:

    This project view is the most useful way of looking at the project while developing it. You can try the Files tab, to see what files there are (similar to what you can see in Windows Explorer):

    but then go back to the Projects tab.
  6. Notice that you have a Source Packages branch in the tree, containing just a default package, currently empty. Also there is a Libraries branch which should contain the current JDK (if it does not, highlight the project name in the tree, Ex16, right-click on it and from the pop-up menu select Properties, at the bottom; the resulting Project properties dialogue has a section for libraries, where you can set the Java platform).
    We will not use the Test Packages or Test Libraries until we get on to unit testing later in the course, so forget about those for now.
  7. Right-click on the Source Packages branch in the project tree. From the pop-up menu select New and then Java package...
  8. In the New Java package dialogue type net.grelf.course as the package name and click the Finish button. You will see that the package now exists in the project tree, under Source Packages.
  9. Right-click on that new package and select New and then Java class...
  10. In the New Java class dialogue type TextFileReader as the class name and click the Finish button. The class appears in the project tree under its package. Also the skeleton of its source code opens in the editor window on the right:
    	package net.grelf.course;
    
    	/** */
    	public class TextFileReader
    	{
    
    	}
    

    However, it may not be laid out like that. Formatting can be controlled: right-click on the project name (Ex16) in the tree, select Properties, in the left of the Project properties dialogue select Formatting, select Use global options and click Edit global options, select Java as the language and Braces as the category, then I like to have New line for the first 3 drop-downs and Generate for the others; explore for yourself the other formatting options and click OK when happy.
  11. In the editor window, inside the class braces, we are going to put a main() method. Start typing pu (for public) and pause. A warning comes up at the left. Whenever this happens there are two things you might do: (a) with the cursor in the line showing the error, press Alt+Enter (2 keys together); this may result in a list of suggestions popping up so you can use arrows to go up or down the list and hit Enter to accept a suggestion; in the present case the one suggestion (create class pu) is not helpful, so press Esc to get out of the list; (b) press Ctrl+Spacebar (2 keys together); in the present case that causes the word public to be completed for you automatically; if there had been several possibilities for the word, the possibilities would again have been presented as a pop-up list from which the right one could be selected by arrows and then Enter.
    So remember those two key combinations: they can save you a lot of typing! Using long names for variables, classes, etc will no longer be a burden, so make your names really meaningful.
  12. So now go ahead and make a main() method that takes one command line argument (the path to a file) and calls an instance method yet to be written:
    	private boolean output (String filePath)
    
    This method is to return false if the file does not exist, in which case the main method will say that (on the console). Otherwise it returns true and sends the contents of the file to the console. Copy code we had earlier for reading the text file.
    You should be able to do all of that and see how NetBeans helps you when something is wrong or not yet complete. Notice also that markers are put in the project tree and on the tabs at the top of the editor window when files contain errors. Red bars also appear in the right margin of the editor and you can click on any of those to go straight to the relevant source line.
  13. When all the error markers have gone you will be able to run the program. NetBeans will have been compiling in the background to create class file(s) as necessary, so it only remainins to run the compiled code. There are several ways to do that: there is a Run menu, there is a green button on the icons bar, or you can right click on a file and select Run this file. The only snag is how to enter the command line argument (the path to the file). The Project properties dialogue has a Run section which enables you to enter the run-time arguments. You will soon see where the console output goes: below the editor.
Next page | Contents page |