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.
-
Create a Jamon template
HelloTemplate.jamon
:Hello, world!
-
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 therender()
method on the template, providing aWriter
that the template will use to output its content. -
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 -
Process the template:
java org.jamon.compiler.TemplateProcessor --destDir=. HelloTemplate
-
Compile everything:
javac HelloTut1.java HelloTemplate*.java
-
Run it:
java HelloTut1
-
You should see:
Hello, world!
Template Constraints and Variations
An important constraint on calling a Jamon template is that a template instance cannot have itsrender()
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.