Geertjan's Blog

  • December 13, 2011

Registering FreeMarker Templates in NetBeans IDE 7.1

Geertjan Wielenga
Product Manager

There's a simple way to create a new module providing file templates such as "Simple Java File" and "Complex Java File" shown below:

Here's the module structure providing the above two file templates:

The "package-info.java" class (which cannot be created by the New Java Class wizard because that doesn't accept hyphens, so I had to use the New Empty File wizard instead) has this content:

    @TemplateRegistration(folder = "Classes",
    displayName = "#SimpleTemplate_displayName",
    content = "simple/simplefile.java.template",
    scriptEngine = "freemarker"),
    @TemplateRegistration(folder = "Classes",
    displayName = "#ComplexTemplate_displayName",
    content = "complex/complexfile.java.template",
    scriptEngine = "freemarker")
    "SimpleTemplate_displayName=Simple Java file",
    "ComplexTemplate_displayName=Complex Java file"})
package org.fm.file;
import org.netbeans.api.templates.TemplateRegistration;
import org.netbeans.api.templates.TemplateRegistrations;
import org.openide.util.NbBundle.Messages;

As you can see, the class has no class signature and no body. It is an annotated package declaration, as discussed here.

Note that the "scriptEngine" variable has been set to "freemarker", meaning I can define templates with this kind of content:

<#assign licenseFirst = "/*">
<#assign licensePrefix = " * ">
<#assign licenseLast = " */">
<#include "../Licenses/license-${project.license}.txt">
<#if package?? && package != "">
package ${package};
 * @author ${user}
public class ${name} {

When I use the template registered above, the Java class below is generated:

 * To change this template, choose Tools | Templates
 * and open the template in the editor.
package javaapplication12;
 * @author geertjan
public class newsimplefile {

Related tutorials:

One thing missing is a way to register your own iterator for creating the template.

Join the discussion

Comments ( 3 )
  • Jesse Glick Tuesday, December 13, 2011

    Do not use the "Java Class" wizard for making package-info.java; there is a separate wizard "Java Package Info".

    It would be customary to put the registration in the same package as the actual template and description (e.g. org.fm.file.complex).

    Registering an iterator is easy - just put the annotation on the class implementing InstantiatingIterator, and omit the content attribute.

  • guest Tuesday, December 13, 2011

    I need to be adequately trained on net beans. How can that be possible because i live in Nigeria. I am sincerely serious not a spam please get in torch with me


  • guest Wednesday, January 11, 2017

    So I have a directory of files I'd like to add to the New File wizard via the TemplateRegistration. Is there a way to do that without explicitly listing them all? ie. if a new template file is added to the directory rebuilding my application will automatically add it to the template registration? Basically, the internal portion of the @TemplateRegistrations I'd like to contain a for-loop over an array of Files (or register the templates programmatically and without annotations).

Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.