|
| 1 | +# Command line |
| 2 | + |
| 3 | +The formatter comes as a native binary that needs no Java, and as a runnable jar for every platform |
| 4 | +the binaries do not cover. |
| 5 | + |
| 6 | +## Download |
| 7 | + |
| 8 | +Pick the file for your platform from the |
| 9 | +[latest release](https://github.com/openjavaformat/open-java-format/releases/latest). |
| 10 | + |
| 11 | +| Platform | File | |
| 12 | +| --- | --- | |
| 13 | +| Linux x86-64, glibc | `open-java-format-linux-glibc_x86-64` | |
| 14 | +| Linux AArch64, glibc | `open-java-format-linux-glibc_aarch64` | |
| 15 | +| macOS, Apple silicon | `open-java-format-macos_aarch64` | |
| 16 | +| macOS, Intel | `open-java-format-macos_x86-64` | |
| 17 | +| Anything else with Java 21 or later | `open-java-format-2.98.0.1-all.jar` | |
| 18 | + |
| 19 | +There is no native binary for Windows or for musl-based Linux such as Alpine. Use the jar there. |
| 20 | + |
| 21 | +``` sh title="Native binary, here for Apple silicon" |
| 22 | +curl -LO https://github.com/openjavaformat/open-java-format/releases/download/2.98.0.1/open-java-format-macos_aarch64 |
| 23 | +chmod +x open-java-format-macos_aarch64 |
| 24 | +./open-java-format-macos_aarch64 --version |
| 25 | +``` |
| 26 | + |
| 27 | +Rename the file to `open-java-format` and move it to a directory on your `PATH`. The examples below |
| 28 | +assume you did. |
| 29 | + |
| 30 | +!!! note "macOS and files downloaded with a browser" |
| 31 | + |
| 32 | + macOS refuses to run a binary that a browser downloaded. Clear the quarantine flag first with |
| 33 | + `xattr -d com.apple.quarantine open-java-format-macos_aarch64`. A file fetched with `curl` does |
| 34 | + not get the flag. |
| 35 | + |
| 36 | +``` sh title="Runnable jar" |
| 37 | +curl -LO https://github.com/openjavaformat/open-java-format/releases/download/2.98.0.1/open-java-format-2.98.0.1-all.jar |
| 38 | +java -jar open-java-format-2.98.0.1-all.jar --version |
| 39 | +``` |
| 40 | + |
| 41 | +The jar carries its dependencies and the `Add-Exports` entries the formatter needs, so it runs |
| 42 | +without JVM flags. |
| 43 | + |
| 44 | +## Verify the download |
| 45 | + |
| 46 | +Every release has a `checksums_sha256.txt`. Download it next to your file and check: |
| 47 | + |
| 48 | +=== "macOS" |
| 49 | + |
| 50 | + ``` sh |
| 51 | + shasum -a 256 --ignore-missing -c checksums_sha256.txt |
| 52 | + ``` |
| 53 | + |
| 54 | +=== "Linux" |
| 55 | + |
| 56 | + ``` sh |
| 57 | + sha256sum --ignore-missing -c checksums_sha256.txt |
| 58 | + ``` |
| 59 | + |
| 60 | +## Format and check |
| 61 | + |
| 62 | +Pass `--ojf` every time. Without a style flag the formatter uses Google Java Style, and the old |
| 63 | +`--palantir` flag is no longer accepted. |
| 64 | + |
| 65 | +``` sh title="Format files in place" |
| 66 | +open-java-format --ojf --replace src/main/java/com/example/Hello.java |
| 67 | +``` |
| 68 | + |
| 69 | +``` sh title="Format every tracked Java file" |
| 70 | +open-java-format --ojf --replace $(git ls-files '*.java') |
| 71 | +``` |
| 72 | + |
| 73 | +``` sh title="Check without changing anything" |
| 74 | +open-java-format --ojf --dry-run --set-exit-if-changed $(git ls-files '*.java') |
| 75 | +``` |
| 76 | + |
| 77 | +The check prints the files that would change and exits with 1 if there are any, which is what a CI |
| 78 | +step needs. |
| 79 | + |
| 80 | +``` sh title="Format standard input" |
| 81 | +cat Hello.java | open-java-format --ojf - |
| 82 | +``` |
| 83 | + |
| 84 | +## Options |
| 85 | + |
| 86 | +| Option | What it does | |
| 87 | +| --- | --- | |
| 88 | +| `--ojf` | Use the open-java-format style: 120 columns, 4-space indents | |
| 89 | +| `--replace`, `-i` | Write the result back to the files instead of printing it | |
| 90 | +| `--dry-run`, `-n` | Print the files that would change, change nothing | |
| 91 | +| `--set-exit-if-changed` | Exit with 1 if anything would change | |
| 92 | +| `-` | Format standard input to standard output | |
| 93 | +| `--lines 5:10` | Format only these lines, counted from 1 | |
| 94 | +| `--fix-imports-only` | Sort imports and remove unused ones, format nothing else | |
| 95 | +| `--skip-sorting-imports` | Leave the import order alone | |
| 96 | +| `--skip-removing-unused-imports` | Keep unused imports | |
| 97 | +| `--skip-reflowing-long-strings` | Do not rewrap string literals that pass the column limit | |
| 98 | +| `@file` | Read options and file names from a file | |
| 99 | +| `--version`, `--help` | Print the version, or every option | |
0 commit comments