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)
For standalone command-line tools with plenty of configuration options to fit your code formatting style, you can try:

* [astyle](http://astyle.sourceforge.net/)
* [ClangFormat](https://clang.llvm.org/docs/ClangFormat.html)

They are both free, open-source, and can format more than just Java source code (C, C++, C#, etc.)