1*10816b52SSorin Basca# google-java-format 2*10816b52SSorin Basca 3*10816b52SSorin Basca`google-java-format` is a program that reformats Java source code to comply with 4*10816b52SSorin Basca[Google Java Style][]. 5*10816b52SSorin Basca 6*10816b52SSorin Basca[Google Java Style]: https://google.github.io/styleguide/javaguide.html 7*10816b52SSorin Basca 8*10816b52SSorin Basca## Using the formatter 9*10816b52SSorin Basca 10*10816b52SSorin Basca### from the command-line 11*10816b52SSorin Basca 12*10816b52SSorin Basca[Download the formatter](https://github.com/google/google-java-format/releases) 13*10816b52SSorin Bascaand run it with: 14*10816b52SSorin Basca 15*10816b52SSorin Basca``` 16*10816b52SSorin Bascajava -jar /path/to/google-java-format-${GJF_VERSION?}-all-deps.jar <options> [files...] 17*10816b52SSorin Basca``` 18*10816b52SSorin Basca 19*10816b52SSorin BascaThe formatter can act on whole files, on limited lines (`--lines`), on specific 20*10816b52SSorin Bascaoffsets (`--offset`), passing through to standard-out (default) or altered 21*10816b52SSorin Bascain-place (`--replace`). 22*10816b52SSorin Basca 23*10816b52SSorin BascaTo reformat changed lines in a specific patch, use 24*10816b52SSorin Basca[`google-java-format-diff.py`](https://github.com/google/google-java-format/blob/master/scripts/google-java-format-diff.py). 25*10816b52SSorin Basca 26*10816b52SSorin Basca***Note:*** *There is no configurability as to the formatter's algorithm for 27*10816b52SSorin Bascaformatting. This is a deliberate design decision to unify our code formatting on 28*10816b52SSorin Bascaa single format.* 29*10816b52SSorin Basca 30*10816b52SSorin Basca### IntelliJ, Android Studio, and other JetBrains IDEs 31*10816b52SSorin Basca 32*10816b52SSorin BascaA 33*10816b52SSorin Basca[google-java-format IntelliJ plugin](https://plugins.jetbrains.com/plugin/8527) 34*10816b52SSorin Bascais available from the plugin repository. To install it, go to your IDE's 35*10816b52SSorin Bascasettings and select the `Plugins` category. Click the `Marketplace` tab, search 36*10816b52SSorin Bascafor the `google-java-format` plugin, and click the `Install` button. 37*10816b52SSorin Basca 38*10816b52SSorin BascaThe plugin will be disabled by default. To enable it in the current project, go 39*10816b52SSorin Bascato `File→Settings...→google-java-format Settings` (or `IntelliJ 40*10816b52SSorin BascaIDEA→Preferences...→Other Settings→google-java-format Settings` on macOS) and 41*10816b52SSorin Bascacheck the `Enable google-java-format` checkbox. (A notification will be 42*10816b52SSorin Bascapresented when you first open a project offering to do this for you.) 43*10816b52SSorin Basca 44*10816b52SSorin BascaTo enable it by default in new projects, use `File→Other Settings→Default 45*10816b52SSorin BascaSettings...`. 46*10816b52SSorin Basca 47*10816b52SSorin BascaWhen enabled, it will replace the normal `Reformat Code` and `Optimize Imports` 48*10816b52SSorin Bascaactions. 49*10816b52SSorin Basca 50*10816b52SSorin Basca#### IntelliJ JRE Config 51*10816b52SSorin Basca 52*10816b52SSorin BascaThe google-java-format plugin uses some internal classes that aren't available 53*10816b52SSorin Bascawithout extra configuration. To use the plugin, go to `Help→Edit Custom VM 54*10816b52SSorin BascaOptions...` and paste in these lines: 55*10816b52SSorin Basca 56*10816b52SSorin Basca``` 57*10816b52SSorin Basca--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED 58*10816b52SSorin Basca--add-exports=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED 59*10816b52SSorin Basca--add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED 60*10816b52SSorin Basca--add-exports=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED 61*10816b52SSorin Basca--add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED 62*10816b52SSorin Basca--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED 63*10816b52SSorin Basca``` 64*10816b52SSorin Basca 65*10816b52SSorin BascaOnce you've done that, restart the IDE. 66*10816b52SSorin Basca 67*10816b52SSorin Basca### Eclipse 68*10816b52SSorin Basca 69*10816b52SSorin BascaThe latest version of the `google-java-format` Eclipse plugin can be downloaded 70*10816b52SSorin Bascafrom the [releases page](https://github.com/google/google-java-format/releases). 71*10816b52SSorin BascaDrop it into the Eclipse 72*10816b52SSorin Basca[drop-ins folder](http://help.eclipse.org/neon/index.jsp?topic=%2Forg.eclipse.platform.doc.isv%2Freference%2Fmisc%2Fp2_dropins_format.html) 73*10816b52SSorin Bascato activate the plugin. 74*10816b52SSorin Basca 75*10816b52SSorin BascaThe plugin adds a `google-java-format` formatter implementation that can be 76*10816b52SSorin Bascaconfigured in `Window > Preferences > Java > Code Style > Formatter > Formatter 77*10816b52SSorin BascaImplementation`. 78*10816b52SSorin Basca 79*10816b52SSorin Basca### Third-party integrations 80*10816b52SSorin Basca 81*10816b52SSorin Basca* Gradle plugins 82*10816b52SSorin Basca * [spotless](https://github.com/diffplug/spotless/tree/main/plugin-gradle#google-java-format) 83*10816b52SSorin Basca * [sherter/google-java-format-gradle-plugin](https://github.com/sherter/google-java-format-gradle-plugin) 84*10816b52SSorin Basca* Apache Maven plugins 85*10816b52SSorin Basca * [spotless](https://github.com/diffplug/spotless/tree/main/plugin-maven#google-java-format) 86*10816b52SSorin Basca * [spotify/fmt-maven-plugin](https://github.com/spotify/fmt-maven-plugin) 87*10816b52SSorin Basca * [talios/googleformatter-maven-plugin](https://github.com/talios/googleformatter-maven-plugin) 88*10816b52SSorin Basca * [Cosium/maven-git-code-format](https://github.com/Cosium/maven-git-code-format): 89*10816b52SSorin Basca A maven plugin that automatically deploys google-java-format as a 90*10816b52SSorin Basca pre-commit git hook. 91*10816b52SSorin Basca* SBT plugins 92*10816b52SSorin Basca * [sbt/sbt-java-formatter](https://github.com/sbt/sbt-java-formatter) 93*10816b52SSorin Basca* [Github Actions](https://github.com/features/actions) 94*10816b52SSorin Basca * [googlejavaformat-action](https://github.com/axel-op/googlejavaformat-action): 95*10816b52SSorin Basca Automatically format your Java files when you push on github 96*10816b52SSorin Basca 97*10816b52SSorin Basca### as a library 98*10816b52SSorin Basca 99*10816b52SSorin BascaThe formatter can be used in software which generates java to output more 100*10816b52SSorin Bascalegible java code. Just include the library in your maven/gradle/etc. 101*10816b52SSorin Bascaconfiguration. 102*10816b52SSorin Basca 103*10816b52SSorin Basca`google-java-format` uses internal javac APIs for parsing Java source. The 104*10816b52SSorin Bascafollowing JVM flags are required when running on JDK 16 and newer, due to 105*10816b52SSorin Basca[JEP 396: Strongly Encapsulate JDK Internals by Default](https://openjdk.java.net/jeps/396): 106*10816b52SSorin Basca 107*10816b52SSorin Basca``` 108*10816b52SSorin Basca--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED 109*10816b52SSorin Basca--add-exports=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED 110*10816b52SSorin Basca--add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED 111*10816b52SSorin Basca--add-exports=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED 112*10816b52SSorin Basca--add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED 113*10816b52SSorin Basca--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED 114*10816b52SSorin Basca``` 115*10816b52SSorin Basca 116*10816b52SSorin Basca#### Maven 117*10816b52SSorin Basca 118*10816b52SSorin Basca```xml 119*10816b52SSorin Basca<dependency> 120*10816b52SSorin Basca <groupId>com.google.googlejavaformat</groupId> 121*10816b52SSorin Basca <artifactId>google-java-format</artifactId> 122*10816b52SSorin Basca <version>${google-java-format.version}</version> 123*10816b52SSorin Basca</dependency> 124*10816b52SSorin Basca``` 125*10816b52SSorin Basca 126*10816b52SSorin Basca#### Gradle 127*10816b52SSorin Basca 128*10816b52SSorin Basca```groovy 129*10816b52SSorin Bascadependencies { 130*10816b52SSorin Basca implementation 'com.google.googlejavaformat:google-java-format:$googleJavaFormatVersion' 131*10816b52SSorin Basca} 132*10816b52SSorin Basca``` 133*10816b52SSorin Basca 134*10816b52SSorin BascaYou can then use the formatter through the `formatSource` methods. E.g. 135*10816b52SSorin Basca 136*10816b52SSorin Basca```java 137*10816b52SSorin BascaString formattedSource = new Formatter().formatSource(sourceString); 138*10816b52SSorin Basca``` 139*10816b52SSorin Basca 140*10816b52SSorin Bascaor 141*10816b52SSorin Basca 142*10816b52SSorin Basca```java 143*10816b52SSorin BascaCharSource source = ... 144*10816b52SSorin BascaCharSink output = ... 145*10816b52SSorin Bascanew Formatter().formatSource(source, output); 146*10816b52SSorin Basca``` 147*10816b52SSorin Basca 148*10816b52SSorin BascaYour starting point should be the instance methods of 149*10816b52SSorin Basca`com.google.googlejavaformat.java.Formatter`. 150*10816b52SSorin Basca 151*10816b52SSorin Basca## Building from source 152*10816b52SSorin Basca 153*10816b52SSorin Basca``` 154*10816b52SSorin Bascamvn install 155*10816b52SSorin Basca``` 156*10816b52SSorin Basca 157*10816b52SSorin Basca## Contributing 158*10816b52SSorin Basca 159*10816b52SSorin BascaPlease see [the contributors guide](CONTRIBUTING.md) for details. 160*10816b52SSorin Basca 161*10816b52SSorin Basca## License 162*10816b52SSorin Basca 163*10816b52SSorin Basca```text 164*10816b52SSorin BascaCopyright 2015 Google Inc. 165*10816b52SSorin Basca 166*10816b52SSorin BascaLicensed under the Apache License, Version 2.0 (the "License"); you may not 167*10816b52SSorin Bascause this file except in compliance with the License. You may obtain a copy of 168*10816b52SSorin Bascathe License at 169*10816b52SSorin Basca 170*10816b52SSorin Basca http://www.apache.org/licenses/LICENSE-2.0 171*10816b52SSorin Basca 172*10816b52SSorin BascaUnless required by applicable law or agreed to in writing, software 173*10816b52SSorin Bascadistributed under the License is distributed on an "AS IS" BASIS, WITHOUT 174*10816b52SSorin BascaWARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 175*10816b52SSorin BascaLicense for the specific language governing permissions and limitations under 176*10816b52SSorin Bascathe License. 177*10816b52SSorin Basca``` 178