Search

Dark theme | Light theme
Showing posts with label NetBeans 6.5. Show all posts
Showing posts with label NetBeans 6.5. Show all posts

June 4, 2009

Taglib uri suggestions in NetBeans

When we develop web applications with Java Server Pages (JSP) we probably need taglib declartions like this:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" %>

NetBeans help us with selecting the correct uri attribute value. NetBeans shows a list of suggestions to use for the URI attribute. Besides the standard Sun tag libraries shows NetBeans also tag libraries we defined ourselves in our project:

May 26, 2009

Automatically generate Javadoc skeleton code in NetBeans

NetBeans can help us with generating a starting point for Javadoc documentation. Suppose we have the following simple Java class:

package javaapplication16;

public class Main {

    public static void main(String[] args) {
        final Main app = new Main();
        app.sayHello("mrhaki");
    }

    public void sayHello(final String name) {
        System.out.println("Hello " + name);
    }

}

We right-click on the Sources Packages and select Tools | Analyze Javadoc.

NetBeans generates a list of all sources files with missing javadoc in the Analyzer window:

We select the files and methods and then hit the Fix Selected button to let NetBeans generate skeleton javadoc code:

package javaapplication16;

/**
 * 
 * @author mrhaki
 */
public class Main {

    /**
     * 
     * @param args
     */
    public static void main(String[] args) {
        final Main app = new Main();
        app.sayHello("mrhaki");
    }

    /**
     * 
     * @param name
     */
    public void sayHello(final String name) {
        System.out.println("Hello " + name);
    }

}

Now all we have to do is provide meaningful documentation.

May 20, 2009

Remove trailing spaces in NetBeans

To remove trailing spaces in a source file we go to Source | Remove Trailing Spaces. And that's it.

May 18, 2009

Create new locale properties files with NetBeans

Developing an application with localized messages is something I need to do now and then. Most of the time the messages are defined in a properties file. For each locale we need to create a properties files with the messages. For example if we have a main properties file messages.properties and we want to define messages for the locale nl we must create the file messages_nl.properties. NetBeans supports this scenario with simple commands to quickly create new properties files for different locales.

Suppose we have created a main locale properties file messages.properties. We right-click on the file and select Add | Locale... from the pop-up menu:

NetBeans opens a new dialog window. Here we can select a locale from the list of locales or fill in the correct values ourselves.

When we click the OK button we have a new messages_nl_NL.properties file. In our Project Navigator we can see the new locale as node for the messages.properties file:

May 14, 2009

Switch between class and test in NetBeans

When we have a Java class in our NetBeans project with a corresponding JUnit test class we can easily switch between them. When we are editing our Java class file and want to go to the test class we select Navigate | Go to Test (Ctrl+Shift+T in Windows). NetBeans jumps to our test class!

To go the other way, we must have our test class open. Now we select Navigate | Go to Tested Class (Ctrl+Shift+T in Windows) and NetBeans jumps to the Java class.

NetBeans assumes the name of the test class starts with the Java class name we want to test, and the package in which both the test class and Java class reside are the same. So for example if we have a Java class com.mrhaki.netbeans.Sample, the test class must be com.mrhaki.netbeans.SampleTest. The files don't have to be in the same directory structure. So the sources can be in a src/ directory and the test classes in a test/ directory.

May 13, 2009

Working with properties files in NetBeans

Properties files can be part of our Java applications, for example to store configuration settings. In NetBeans we create a new properties file via File | New File... | Other | Properties File. To add properties we simply open the file and add properties as a key=value pair in the editor. But we can use another method: right-click on the properties file and select Add | Property...:

NetBeans opens a small dialog window and we can type the name, value and a comment (optional) for the new property. We click on the OK button to insert the property in our file.

In the Project Navigator we can view all properties in a properties file by clicking on the little arrow in front of the file. We select the property and the editor jumps to right position in the file.

May 12, 2009

Quickly insert constructors from super class in NetBeans

To quickly insert constructors from a super class in NetBeans we only have to place the cursor in our class and go to Source | Insert Code... (or press Alt+Insert in Windows). For example we create a new Exception class for our application extending the java.lang.Exception class:

package com.mrhaki.netbeans;

public class AppException extends Exception {
    
}

NetBeans shows a pop-up window and we select Constructor....

We get a dialog window with all constructors from the java.lang.Exception class. We select the constructors we want and click on the Generate button.

Now our source code looks like this:

package com.mrhaki.netbeans;

public class AppException extends Exception {

    public AppException(Throwable cause) {
        super(cause);
    }

    public AppException(String message, Throwable cause) {
        super(message, cause);
    }

    public AppException(String message) {
        super(message);
    }

    public AppException() {
    }
    
}

May 6, 2009

Skipping tests from Maven build in NetBeans

In NetBeans we can disable unit testing in a Maven project. I've been working on a project lately with a lot of different modules. Some of these modules contained failing unit test, but there was nothing I could do about it. In order to keep building the modules we can disable the unit testing.

We can define a custom goal for our Maven project. Therefore we must right-click on the project and select Custom | Goals.... NetBeans opens a dialog window. Here we select the checkbox Skip Tests to disable unit testing. We can save this goal with the name build (skip tests).

Now we have the custom goal available if we right-click on the project and go to Custom.

Another way to disable testing is go to Project Properties | Actions. We get the list of actions NetBeans uses and the Maven goals belonging to the actions. We select the Build project action and check the Skip tests checkbox. Now NetBeans will disable unit testing when we invoke the Build project command.

April 14, 2009

Go full screen with NetBeans

When we want to get the most of our big, high resolution screens in Windows we can maximize the NetBeans window. But this will still leave the title bar on the screen, so to really use all of the screen we can run NetBeans in full screen mode. We go to View | Full Screen (or use Alt+Shift+ENTER in Windows) and NetBeans goes full screen. The title bar is now gone. And if we go to Window | Maximize Window (or use Shift+ESC in Windows) we can maximize the editor window to get a big editing space:

April 8, 2009

Find Grails files and types in NetBeans

We can search quickly for Groovy and resource files in our Grails application in NetBeans. We go to Navigate | Go to Type... or use the (Windows) shortcut key Ctrl+O. NetBeans opens a dialog in which we type the name of the Groovy type we are looking for. For example we can look for the file Config.groovy by typing the letters conf. We then select the found file and click on the OK button to open the file in NetBeans:

To search for other files in our Grails application we must go to Navigate | Go to File... or use the (Windows) shortcut key Alt+Shift+O. In this dialog window we can type part of the name of the file we are looking for. For example we can type in to search for the index.gsp file:

April 3, 2009

See which processes are running in NetBeans

Sometimes NetBeans starts up processes in the background. For example to scan all projects for tasks or downloading indexes for a Maven repository. At the right side of the status bar we can see a progress bar when this happens:

We can click on the progress bar to see which processes are running. Another way is to select Windows | Processes from the menubar. NetBeans shows a stack of all processes that are running:

Install Grails plugins in NetBeans

Update for NetBeans 6.7.1

The great thing about Grails is that we can extend Grails easily with plugins. Plugins extend Grails with new functionality in a Grails way. To install a plugin for our Grails application is NetBeans is easy.

In NetBeans we right-click on the application in the Project Navigator:

We select the option Plugins from the pop-up menu and NetBeans opens a dialog window where we can see which plugins are already installed for our application and a tab page New plugin. When we click on the New Plugin tab we get a list of available plugins. We can select the plugin we want (for example Searchable) and click on the Install button:

The plugin is now downloaded and installed in our appliation:

When we return to the Installed tab we see our newly installed plugin:

April 2, 2009

Change character encoding scheme for Maven resource and compile plugin in NetBeans

In a previous post we have learned how to change the source version for the Maven compiler plugin. But we can also change the character encoding scheme from the same Project Properties dialog window. The Encoding select box let's us change the encoding for the Maven project.

NetBeans add an encoding element to the compiler plugin definition and adds a configuration for the resource plugin in the pom.xml.

<build>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.0.2</version>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
                <encoding>UTF-8</encoding>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.2</version>
            <configuration>
                <encoding>UTF-8</encoding>
            </configuration>
        </plugin>
    </plugins>
</build>

April 1, 2009

Change source version for Maven compiler plugin in NetBeans

The Maven compiler plugin allows us to change the source version of the Java files for compilation. If we want to use all the latest support, like assertions, we must set the version to at least 1.4. If we want to use annotations we must set the value to at least 1.5.

In NetBeans we can change this in the Project Properties window. We right-click on our Maven project and select Project Properties. In the Sources tab we can change the Source/Binary Format to the version we want.

NetBeans add the compiler plugin to the pom.xml with the selected version:

<build>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.0.2</version>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
            </configuration>
        </plugin>
    </plugins>
</build>

March 31, 2009

Connect lifelines in UML Sequence diagrams in NetBeans

NetBeans has support for UML diagrams. We can create Activity, Class, Sequence, State and Use Case diagrams. In this post we will see how NetBeans helps with creating a sequence diagram.

To start we must create a new project. We go to File | New Project. From the dialog window we select the UML category. We can create three types of projects: Platform-Independent, Java-Platform, Reverse Engineered Java-Platform. For our example we select Platform-Independent Model and click on the Next button.

We can now choose a name for our project and the location. After we filled in the values we select the Finish button.

NetBeans creates a new project for us. The project node in the Projects view shows a Diagrams node. We can right-click on Diagrams node and select New | Diagram from the pop-up menu:

A dialog window is opened and we can select the type of diagram. Here we select Sequence Diagram and press the Finish button:

Netbeans shows us a empty sequence diagram. We can add lifelines to the diagram by opening the Palette and select Lifeline and we can drop it on the diagram:

Once we have lifelines on the diagram we can connect them. We select a lifeline on the diagram and NetBeans shows a small pop-up window with options to create synchronous, asynchronous and create messages:

When we click on the Synchronous Message option a new lifeline is added to the diagram with a message interaction between the new and old lifeline:

To connect the synchronous message to an existing lifeline we must simply hold the mouse button and drag the arrow to the existing lifeline. When we release the mouse button the message interaction is created:

March 25, 2009

Go to NetBeans website directly from NetBeans

To visit the NetBeans website we only have to go to View | Web Browser. NetBeans starts our web browser with the URL http://www.netbeans.org.

March 5, 2009

Create a Maven project site in NetBeans

Within a Maven project it is easy to create a project site. We need to create the src/site directory and place a site.xml file in it. Here is a very simple and basic site.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project name="app"/>

And of course we can add content files for the project site. To generate the site we only have to run mvn site. And if we use NetBeans we can generate the site with a mouse click.

In our Maven project we have a Project site node. If we right-click on the node we get the following pop-up menu:

We notice the options Generate Site and Deploy Site. If we select Generate Site we get a dialog window with the Maven command that is about to run:

We could change some fields here, but most of the time we just press the OK button. And the project site is generated. The generated site is placed in the target/site directory. If we look at the output window and see the Maven output we notice a link View Generated Project Site:

We can click on this link and NetBeans will open the site's index.html file in a web browser.

We can also deploy the project site to a web server with the Deploy Site option from the pop-up menu. This command is executed immediately by NetBeans and the if the configuration in our pom.xml is correct, the site is deploy to a web server.

February 14, 2009

Enable JSP editor features for Maven webapp project in NetBeans

When we create a new Maven webapp project in NetBeans the JSP editing features are not enabled. If we open a JSP file we see the following error: Servlet API classes are not on the classpath, some scriplet editing features are disabled.

We need to add the Servlet API classes ourselves to the dependencies of our project. When the classes are in the classpath of our project we get complete code completion in our JSP editor. So how do we add the API classes? First we go to Window | Other | | Maven Repository Browser. We click on the Find icon on the right to search for the Servlet API. In the search dialog window we type javax.servlet and press the OK button:

NetBeans shows the search results in the Maven Repository Browser:

We need at least javax.servlet:servlet-api. We open this node and select versions 2.5[jar]. We right-click with the mouse and select Add Dependency to:

NetBeans opens a dialog window where we select our Maven webapp project and click the Add button.

This is enough to get some code completion in the JSP editor. To also get support for EL expressions we add the following libraries: javax.servlet.jsp:jsp-api and javax.servlet:jstl. Now when we return to our JSP editor we see how we can use code completion:

Finally to we can edit our pom.xml and set the scope of the added libaries to provided, because the application server we deploy the application to will already have these classes in the classpath.

February 11, 2009

Unset main project in NetBeans

In NetBeans we can set a project to be a Main Project. We can use shortcut keys to run or debug a main project even if we are working on another project. To unset the Main Project we must go to Run | Set Main Project | None.

Use Jetty to run Maven web applications in NetBeans

Jetty is a great web server. It is fast to start up and that makes it very useful during development. Jetty has a Maven plugin we can use in our Maven web application project. This way we can use Jetty to test our JSP files. We can change the JSP file and hit the Refresh button of our browser to see the result immediately. We can even compile a Java class file and Jetty will restart so we can test the result in our browser.

Let's see how we can do this in NetBeans. We go to File | New Project, select Maven Project and click the Next button.

In the next dialog window we select Maven Webapp Archetype and click the Next button.

Now we can fill in the values for our project and press the Finish button.

NetBeans creates a new project for us. To add Jetty Maven plugin we must open the pom.xml file for our project. In the build section we must add the following code:

<plugins>
 <plugin>
  <groupId>org.mortbay.jetty</groupId>
  <artifactId>maven-jetty-plugin</artifactId>
  <version>6.1.14</version>
  <configuration>
   <scanIntervalSeconds>5</scanIntervalSeconds>
  </configuration>
 </plugin>
</plugins>

At line 6 we define how often Jetty will scan the source directories to look for changes. Because we add this definition we can change JSP files and see the result in our web browser without restarting the server.

Okay, we have defined Jetty for our project, but how can we run Jetty in NetBeans? We go to File | Project Properties and select the Actions category. We must look for the action Run project and select it. In the Execute goals field we add jetty:run. This will start Jetty if we run our project. We can close the dialog window.

Now we go to Run | Run Project to start Jetty. In the Output window of NetBeans we see the following text:

Starting jetty 6.1.14 ...
2009-02-11 10:20:20.610::INFO:  jetty-6.1.14
2009-02-11 10:20:20.982::INFO:  No Transaction manager found - if your webapp requires one, please configure one.
2009-02-11 10:20:21.335::INFO:  Started SelectChannelConnector@0.0.0.0:8080
Started Jetty Server
Starting scanner at interval of 5 seconds.

At line 3 we see Started SelectChannelConnector@0.0.0.0:8080 this means Jetty is available at port 8080. We open a web browser and open http://localhost:8080/webappjetty/ and we see the following:

We go back to NetBeans and open the file index.jsp. We add the following to the file:

<h3>It works!</h3>

We switch back to our web browser and reload the page and we get: