type safe templating

  Tutorial contents Next

Jamon Tutorial - 1. Getting Started with Hello World

The source files mentioned in this tutorial are available individually where first referenced, or bundled in a zip file.
NOTE: If you have a Java Runtime (JRE) installed in addition to a Software Development Kit (SDK), be sure that the binaries (i.e. java, javac, etc) of the SDK come first in your PATH.
  1. Create a Jamon template HelloTemplate.jamon:
    Hello, world!
  2. Create a java file HelloTut1.java:
    import java.io.OutputStreamWriter;
    public class HelloTut1 {
      public static void main(String[] argv) throws Exception {
        new HelloTemplate().render(new OutputStreamWriter(System.out));
    The code then calls the render() method on the template, providing a Writer that the template will use to output its content.
  3. Set your classpath:
    Windows C:\JAMONTMP> SET CLASSPATH=.;\path\to\jamon-runtime.jar;\path\to\jamon-api.jar;\path\to\jamon-processor.jar
    Unix (sh, bash, zsh, ksh) $ export CLASSPATH=.:/path/to/jamon-runtime.jar:/path/to/jamon-api.jar:/path/to/jamon-processor.jar
    Unix (csh, tcsh) % setenv CLASSPATH=.:/path/to/jamon-runtime.jar:/path/to/jamon-api.jar:/path/to/jamon-processor.jar
  4. Process the template:
    java org.jamon.compiler.TemplateProcessor --destDir=. HelloTemplate
  5. Compile everything:
    javac HelloTut1.java HelloTemplate*.java
  6. Run it:
    java HelloTut1
  7. You should see:
    Hello, world!

Template Constraints and Variations

An important constraint on calling a Jamon template is that a template instance cannot have its render() method called twice. Instead, the user must create a new template instance (a cheap operation) each time the template is to be displayed.
    OutputWriter ouputWriter = new OutputStreamWriter(System.out);
    new HelloTemplate().render(outputWriter);

    // do more work

    // create a NEW template instance
    new HelloTemplate().render(outputWriter);

If the above code had called the HelloTemplate instance's render() method twice, Jamon would have generated an exception.

  Tutorial contents Next