Question: I'm using FreeMarker to generate java code, but as most of it is dynamically generated it's difficult to control the code formation. 

I want to get code well formatted. Does anyone knows a lib or something like a pretty printer for java code?

Answer: 
Jalopy works beautifully. You can use it's CLI for standalone usage. [Japlopy Console Plugin](http://jalopy.sourceforge.net/existing/plugin-console.html)
You can format it when you edit the .java file in Eclipse. 
When you don't edit it, it doesn't matter if it is formatted or not.