Netbeans Tutorial

0 views
Skip to first unread message

Luz Ignasiak

unread,
Aug 4, 2024, 9:50:19 PM8/4/24
to tramunibeat
Its time to write your first application! These detailed instructions are for users of the NetBeans IDE. The NetBeans IDE runs on the Java platform, which means that you can use it with any operating system for which there is a JDK available. These operating systems include Microsoft Windows, Solaris OS, Linux, and Mac OS X.

When you create an IDE project, you create an environment in which to build and run your applications. Using IDE projects eliminates configuration issues normally associated with developing on the command line. You can build or run your application by choosing a single menu item within the IDE.


A source file contains code, written in the Java programming language, that you and other programmers can understand. As part of creating an IDE project, a skeleton source file will be automatically generated. You will then modify the source file to add the "Hello World!" message.


The IDE invokes the Java programming language compiler (javac), which takes your source file and translates its text into instructions that the Java virtual machine can understand. The instructions contained within this file are known as bytecodes.


To set this JDK as the default for all projects, you can run the IDE with the --jdkhome switch on the command line, or by entering the path to the JDK in the netbeans_j2sdkhome property of your INSTALLATION_DIRECTORY/etc/netbeans.conf file.


When you created this project, you left the Create Main Class checkcbox selected in the New Project wizard. The IDE has therefore created a skeleton class for you. You can add the "Hello World!" message to the skeleton code by replacing the line:


If the build output concludes with the statement BUILD FAILED, you probably have a syntax error in your code. Errors are reported in the Output window as hyperlinked text. You double-click such a hyperlink to navigate to the source of an error. You can then fix the error and once again choose Run Build Project.


When you build the project, the bytecode file HelloWorldApp.class is generated. You can see where the new file is generated by opening the Files window and expanding the Hello World App/build/classes/helloworldapp node as shown in the following figure.


The next few pages of the tutorial will explain the code in this simple application. After that, the lessons go deeper into core language features and provide many more examples. Although the rest of the tutorial does not give specific instructions about using the NetBeans IDE, you can easily use the IDE to write and run the sample code. The following are some tips on using the IDE and explanations of some IDE behavior that you are likely to see:


Once you have created a project in the IDE, you can add files to the project using the New File wizard. Choose File New File, and then select a template in the wizard, such as the Empty Java File template.


You can compile and run an individual file (as opposed to a whole project) using the IDE's Compile File (F9) and Run File (Shift-F6) commands. If you use the Run Main Project command, the IDE will run the file that the IDE associates as the main class of the main project. Therefore, if you create an additional class in your HelloWorldApp project and then try to run that file with the Run Main Project command, the IDE will run the HelloWorldApp file instead.


As you are typing in the IDE, a code completion box might periodically appear. You can either ignore the code completion box and keep typing, or you can select one of the suggested expressions. If you would prefer not to have the code completion box automatically appear, you can turn off the feature. Choose Tools Options Editor, click the Code Completion tab and clear the Auto Popup Completion Window check box.


If you want to rename the node for a source file in the Projects window, choose Refactor from IDE's main menu. The IDE prompts you with the Rename dialog box to lead you through the options of renaming the class and the updating of code that refers to that class. Make the changes and click Refactor to apply the changes. This sequence of clicks might seem unnecessary if you have just a single class in your project, but it is very useful when your changes affect other parts of your code in larger projects.


Netbeans IDE, started as a student project known as Xelfi in the past, is a popular IDE developed with the goal to create a Delphi like IDE for Java. First developed in 1996, it has grown into a full fledged IDE for Enterprise scaled software development. With its excellent integrated abilities like the connection manager, integrated Glassfish server and resource managers, Netbeans IDE makes development quite easy for novice developers. This article has been created with the goal to guide every developer on how to take the most out of Netbeans IDE and its features.


For the tutorial, we would be downloading Netbeans IDE for Java EE. This would require Java 8 pre-installed to prevent any roadblocks during the tutorial. The Java installation version could be verified by using the command java --version in the command prompt or terminal depending on your OS.


Netbeans IDE is available in various flavours shown below. For the purpose of this tutorial, we would proceed with the download of Java EE version of Netbeans IDE 8.2. The download list could be found here. It is available for all the popular operating systems and unlike other IDEs, there is also an OS independent version of Netbeans available for download. For now, we will proceed with the download of OS specific version.


Once downloaded, the installation process is quite straightforward. Simply run the setup and follow the installation process. Once installed, start the IDE. It might take a while to load all the modules. Once loaded, you should be welcomed by this screen if everything went perfect with the installation.


The services tab holds a list of Databases, sample RESTful services, Servers, Repositories, Test drivers and others. These services allow you to get an interface to test these components and interact with them right away. We will be looking at these as we proceed.


Let us start with the creation of first project. To start with the first project, either navigate to File -> New Project or simply press the New Project button as described above. It should open up a small window as shown below.


For the tutorial purpose, we will proceed with Java Application. Select Java application in the right side panel and click Next. The next step asks for the selection of project name, location and folder. Select the necessary details and click next. The below image shows these details filled in.


On click of Finish, you should be able to see that your main class is automatically created. The project shows up in the Projects window and the MainClass shows up in the editor. A snapshot of how it appears is shown below.


Now click the save button or Navigate to File -> Save to save the file. We have intentionally missed out a semicolon in the above code. When you paste the code in your editor, you would see a red line below the System.out.println("Hello there! Welcome to Java Code Geeks") statement. The red line indicates an error in the statement. Netbeans automatically builds the code in the backend to identify any potential compilation issues in advance. To know more about the error, simply hover over the red mark on the left side of the editor. It will show a tooltip like the one shown below to help you understand the error.


With the tooltip, Netbeans also provides hints. Simply press Alt+Enter. It will take you to the end of the line and suggest what needs to be done. In this manner, Netbeans plays the role of an IDE by dynamically helping you solve the problems. Place a semicolon at the end of the line with error.


Once the code is ready, the next step is to build and execute the project. In the top bar, you would be able to see two buttons to build the project. The buttons are Build and Clean & Build respectively. To explain the purpose of both the buttons, one needs to have a basic understanding of what happens when a project is built.


In case of Java, on building the project, it compiles the Java files into Class files. These files are placed inside a folder named bin in the corresponding project folder. The Clean & Build button clears these class files and recompiles the complete project code again while the Build button, simply compile the files that are necessary. The project can also be built using the shortcuts for these buttons.


These tasks can also be executed by navigating to the Run->Build Project & Run->Clean Build Project menu items respectively. Another possible way is to right click the project and select the relevant option.


Once the project is built successfully, the code would be ready to execute. To execute the project, there are numerous ways. The simplest way is the shortcut key F6. It runs the code with its default configurations. Another possible way out is to use the green play button to execute the code. On executing the code, it should display the output as shown below.


Now, we will understand the process of debugging. Debugging is to execute the code step by step to analyse possible causes of error. In order to debug the code from a specific line, we need to add a breakpoint. To add a breakpoint, simple click on the line number on the left side of the editor as shown below.

3a8082e126
Reply all
Reply to author
Forward
0 new messages