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: 
You can use [Eclipse](http://blogs.operationaldynamics.com/andrew/software/java-gnome/eclipse-code-format-from-command-line.html)'s or [Jalopy](http://jalopy.sourceforge.net/) code formatters to reformat generated code.
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.)