java.md

  1# Java
  2
  3There are two extensions that provide Java language support for Zed:
  4
  5- Zed Java: [zed-extensions/java](https://github.com/zed-extensions/java) and
  6- Java with Eclipse JDTLS: [zed-java-eclipse-jdtls](https://github.com/ABckh/zed-java-eclipse-jdtls).
  7
  8Both use:
  9
 10- Tree-sitter: [tree-sitter/tree-sitter-java](https://github.com/tree-sitter/tree-sitter-java)
 11- Language Server: [eclipse-jdtls/eclipse.jdt.ls](https://github.com/eclipse-jdtls/eclipse.jdt.ls)
 12
 13## Install OpenJDK
 14
 15You will need to install a Java runtime (OpenJDK).
 16
 17- MacOS: `brew install openjdk`
 18- Ubuntu: `sudo add-apt-repository ppa:openjdk-23 && sudo apt-get install openjdk-23`
 19- Windows: `choco install openjdk`
 20- Arch Linux: `sudo pacman -S jre-openjdk-headless`
 21
 22Or manually download and install [OpenJDK 23](https://jdk.java.net/23/).
 23
 24## Extension Install
 25
 26You can install either by opening {#action zed::Extensions}({#kb zed::Extensions}) and searching for `java`.
 27
 28We recommend you install one or the other and not both.
 29
 30## Settings / Initialization Options
 31
 32Both extensions will automatically download the language server, see: [Manual JDTLS Install](#manual-jdts-install) below if you'd prefer to manage that yourself.
 33
 34For available `initialization_options` please see the [Initialize Request section of the Eclipse.jdt.ls Wiki](https://github.com/eclipse-jdtls/eclipse.jdt.ls/wiki/Running-the-JAVA-LS-server-from-the-command-line#initialize-request).
 35
 36You can add these customizations to your Zed Settings by launching {#action zed::OpenSettings}({#kb zed::OpenSettings}) or by using a `.zed/setting.json` inside your project.
 37
 38### Zed Java Settings
 39
 40```json
 41{
 42  "lsp": {
 43    "jdtls": {
 44      "initialization_options": {}
 45    }
 46  }
 47}
 48```
 49
 50### Java with Eclipse JDTLS settings
 51
 52```json
 53{
 54  "lsp": {
 55    "java": {
 56      "settings": {},
 57      "initialization_options": {}
 58    }
 59  }
 60}
 61```
 62
 63## Example Configs
 64
 65### Zed Java Initialization Options
 66
 67There are also many more options you can pass directly to the language server, for example:
 68
 69```json
 70{
 71  "lsp": {
 72    "jdtls": {
 73      "initialization_options": {
 74        "bundles": [],
 75        "workspaceFolders": ["file:///home/snjeza/Project"],
 76        "settings": {
 77          "java": {
 78            "home": "/usr/local/jdk-9.0.1",
 79            "errors": {
 80              "incompleteClasspath": {
 81                "severity": "warning"
 82              }
 83            },
 84            "configuration": {
 85              "updateBuildConfiguration": "interactive",
 86              "maven": {
 87                "userSettings": null
 88              }
 89            },
 90            "trace": {
 91              "server": "verbose"
 92            },
 93            "import": {
 94              "gradle": {
 95                "enabled": true
 96              },
 97              "maven": {
 98                "enabled": true
 99              },
100              "exclusions": [
101                "**/node_modules/**",
102                "**/.metadata/**",
103                "**/archetype-resources/**",
104                "**/META-INF/maven/**",
105                "/**/test/**"
106              ]
107            },
108            "jdt": {
109              "ls": {
110                "lombokSupport": {
111                  "enabled": false // Set this to true to enable lombok support
112                }
113              }
114            },
115            "referencesCodeLens": {
116              "enabled": false
117            },
118            "signatureHelp": {
119              "enabled": false
120            },
121            "implementationsCodeLens": {
122              "enabled": false
123            },
124            "format": {
125              "enabled": true
126            },
127            "saveActions": {
128              "organizeImports": false
129            },
130            "contentProvider": {
131              "preferred": null
132            },
133            "autobuild": {
134              "enabled": false
135            },
136            "completion": {
137              "favoriteStaticMembers": [
138                "org.junit.Assert.*",
139                "org.junit.Assume.*",
140                "org.junit.jupiter.api.Assertions.*",
141                "org.junit.jupiter.api.Assumptions.*",
142                "org.junit.jupiter.api.DynamicContainer.*",
143                "org.junit.jupiter.api.DynamicTest.*"
144              ],
145              "importOrder": ["java", "javax", "com", "org"]
146            }
147          }
148        }
149      }
150    }
151  }
152}
153```
154
155### Java with Eclipse JTDLS Configuration {#zed-java-eclipse-configuration}
156
157Configuration options match those provided in the [redhat-developer/vscode-java extension](https://github.com/redhat-developer/vscode-java#supported-vs-code-settings).
158
159For example, to enable [Lombok Support](https://github.com/redhat-developer/vscode-java/wiki/Lombok-support):
160
161```json
162{
163  "lsp": {
164    "java": {
165      "settings": {
166        "java.jdt.ls.lombokSupport.enabled:": true
167      }
168    }
169  }
170}
171```
172
173## Manual JDTLS Install
174
175If you prefer, you can install JDTLS yourself and both extensions can be configured to use that instead.
176
177- MacOS: `brew install jdtls`
178- Arch: [`jdtls` from AUR](https://aur.archlinux.org/packages/jdtls)
179
180Or manually download install:
181
182- [JDTLS Milestone Builds](http://download.eclipse.org/jdtls/milestones/) (updated every two weeks)
183- [JDTLS Snapshot Builds](https://download.eclipse.org/jdtls/snapshots/) (frequent updates)
184
185## See also
186
187- [Zed Java Readme](https://github.com/zed-extensions/java)
188- [Java with Eclipse JDTLS Readme](https://github.com/ABckh/zed-java-eclipse-jdtls)
189
190## Support
191
192If you have issues with either of these plugins, please open issues on their respective repositories:
193
194- [Zed Java Issues](https://github.com/zed-extensions/java/issues)
195- [Java with Eclipse JDTLS Issues](https://github.com/ABckh/zed-java-eclipse-jdtls/issues)