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