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      "settings": {
 45        "version": "1.40.0", // jdtls version to download and use
 46        "classpath": "/path/to/classes.jar:/path/to/more/classes/"
 47      },
 48      "initialization_options": {}
 49    }
 50  }
 51}
 52```
 53
 54### Java with Eclipse JDTLS settings
 55
 56```json
 57{
 58  "lsp": {
 59    "java": {
 60      "settings": {},
 61      "initialization_options": {}
 62    }
 63  }
 64}
 65```
 66
 67## Example Configs
 68
 69### Zed Java Initialization Options
 70
 71There are also many more options you can pass directly to the language server, for example:
 72
 73```json
 74{
 75  "lsp": {
 76    "jdtls": {
 77      "initialization_options": {
 78        "bundles": [],
 79        "workspaceFolders": ["file:///home/snjeza/Project"],
 80        "settings": {
 81          "java": {
 82            "home": "/usr/local/jdk-9.0.1",
 83            "errors": {
 84              "incompleteClasspath": {
 85                "severity": "warning"
 86              }
 87            },
 88            "configuration": {
 89              "updateBuildConfiguration": "interactive",
 90              "maven": {
 91                "userSettings": null
 92              }
 93            },
 94            "trace": {
 95              "server": "verbose"
 96            },
 97            "import": {
 98              "gradle": {
 99                "enabled": true
100              },
101              "maven": {
102                "enabled": true
103              },
104              "exclusions": [
105                "**/node_modules/**",
106                "**/.metadata/**",
107                "**/archetype-resources/**",
108                "**/META-INF/maven/**",
109                "/**/test/**"
110              ]
111            },
112            "referencesCodeLens": {
113              "enabled": false
114            },
115            "signatureHelp": {
116              "enabled": false
117            },
118            "implementationsCodeLens": {
119              "enabled": false
120            },
121            "format": {
122              "enabled": true
123            },
124            "saveActions": {
125              "organizeImports": false
126            },
127            "contentProvider": {
128              "preferred": null
129            },
130            "autobuild": {
131              "enabled": false
132            },
133            "completion": {
134              "favoriteStaticMembers": [
135                "org.junit.Assert.*",
136                "org.junit.Assume.*",
137                "org.junit.jupiter.api.Assertions.*",
138                "org.junit.jupiter.api.Assumptions.*",
139                "org.junit.jupiter.api.DynamicContainer.*",
140                "org.junit.jupiter.api.DynamicTest.*"
141              ],
142              "importOrder": ["java", "javax", "com", "org"]
143            }
144          }
145        }
146      }
147    }
148  }
149}
150```
151
152### Java with Eclipse JTDLS Configuration {#zed-java-eclipse-configuration}
153
154Configuration options match those provided in the [redhat-developer/vscode-java extension](https://github.com/redhat-developer/vscode-java#supported-vs-code-settings).
155
156For example, to enable [Lombok Support](https://github.com/redhat-developer/vscode-java/wiki/Lombok-support):
157
158```json
159{
160  "lsp": {
161    "java": {
162      "settings": {
163        "java.jdt.ls.lombokSupport.enabled:": true
164      }
165    }
166  }
167}
168```
169
170## Manual JDTLS Install
171
172If you prefer, you can install JDTLS yourself and both extensions can be configured to use that instead.
173
174- MacOS: `brew install jdtls`
175- Arch: [`jdtls` from AUR](https://aur.archlinux.org/packages/jdtls)
176
177Or manually download install:
178
179- [JDTLS Milestone Builds](http://download.eclipse.org/jdtls/milestones/) (updated every two weeks)
180- [JDTLS Snapshot Builds](https://download.eclipse.org/jdtls/snapshots/) (frequent updates)
181
182## See also
183
184- [Zed Java Readme](https://github.com/zed-extensions/java)
185- [Java with Eclipse JDTLS Readme](https://github.com/ABckh/zed-java-eclipse-jdtls)
186
187## Support
188
189If you have issues with either of these plugins, please open issues on their respective repositories:
190
191- [Zed Java Issues](https://github.com/zed-extensions/java/issues)
192- [Java with Eclipse JDTLS Issues](https://github.com/ABckh/zed-java-eclipse-jdtls/issues)