Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Code Formatting

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?

like image 636
the qwerty Avatar asked May 20 '10 17:05

the qwerty


People also ask

How do I format Java code?

Open the required file. Go to Source | Format Document or press Ctrl+Shift+F.

What is formatting in Java?

Java Formatter is a utility class that can make life simple when working with formatting stream output in Java. It is built to operate similarly to the C/C++ printf function. It is used to format and output data to a specific destination, such as a string or a file output stream.

What is code formatting?

Source Code Format means a form of computer program, or any portion thereof, written in a programming language employed by computer programmers that must be compiled or otherwise translated before it can be executed by a computer.

Does formatting matter in Java?

The way in which Java code is formatted and laid out is largely a matter of personal preference. Some styles (such as omitting braces in conditional statements) can arguably make certain types of bug slightly more likely.


2 Answers

Google java format works perfect for me. https://github.com/google/google-java-format

After maven build, locate the google-java-format-0.1-SNAPSHOT.jar in core/target folder, and try java -jar google-java-format-0.1-SNAPSHOT.jar to see usage information.

like image 71
digiter Avatar answered Sep 24 '22 21:09

digiter


You can use Eclipse's or Jalopy code formatters to reformat generated code.

like image 32
Eugene Kuleshov Avatar answered Sep 25 '22 21:09

Eugene Kuleshov