java.md

  1# Java
  2
  3Java language support in Zed is provided by:
  4
  5- Zed Java: [zed-extensions/java](https://github.com/zed-extensions/java)
  6- Tree-sitter: [tree-sitter/tree-sitter-java](https://github.com/tree-sitter/tree-sitter-java)
  7- Language Server: [eclipse-jdtls/eclipse.jdt.ls](https://github.com/eclipse-jdtls/eclipse.jdt.ls)
  8
  9## Install OpenJDK
 10
 11You will need to install a Java runtime (OpenJDK).
 12
 13- macOS: `brew install openjdk`
 14- Ubuntu: `sudo add-apt-repository ppa:openjdk-23 && sudo apt-get install openjdk-23`
 15- Windows: `choco install openjdk`
 16- Arch Linux: `sudo pacman -S jre-openjdk-headless`
 17
 18Or manually download and install [OpenJDK 23](https://jdk.java.net/23/).
 19
 20## Extension Install
 21
 22You can install by opening {#action zed::Extensions}({#kb zed::Extensions}) and searching for `java`.
 23
 24## Quick start and configuration
 25
 26For the majority of users, Java support should work out of the box.
 27
 28- It is generally recommended to open projects with the Zed-project root at the Java project root folder (where you would commonly have your `pom.xml` or `build.gradle` file).
 29
 30- By default the extension will download and run the latest official version of JDTLS for you, but this requires Java version 21 to be available on your system via either the `$JAVA_HOME` environment variable or as a `java(.exe)` executable on your `$PATH`. If your project requires a lower Java version in the environment, you can specify a different JDK to use for running JDTLS via the `java_home` configuration option.
 31
 32- You can provide a **custom launch script for JDTLS**, by adding an executable named `jdtls` (or `jdtls.bat` on Windows) to your `$PATH` environment variable. If this is present, the extension will skip downloading and launching a managed instance and use the one from the environment.
 33
 34- To support [Lombok](https://projectlombok.org/), the lombok-jar must be downloaded and registered as a Java-Agent when launching JDTLS. By default the extension automatically takes care of that, but in case you don't want that you can set the `lombok_support` configuration-option to `false`.
 35
 36Here is a common `settings.json` including the above mentioned configurations:
 37
 38```jsonc
 39{
 40  "lsp": {
 41    "jdtls": {
 42      "settings": {
 43        "java_home": "/path/to/your/JDK21+",
 44        "lombok_support": true,
 45      },
 46    },
 47  },
 48}
 49```
 50
 51## Debugging
 52
 53Debug support is enabled via our [Fork of Java Debug](https://github.com/zed-industries/java-debug), which the extension will automatically download and start for you. Please refer to the [Debugger Documentation](https://zed.dev/docs/debugger#getting-started) for general information about how debugging works in Zed.
 54
 55To get started with Java, click the `edit debug.json` button in the Debug menu, and replace the contents of the file with the following:
 56
 57```jsonc
 58[
 59  {
 60    "adapter": "Java",
 61    "request": "launch",
 62    "label": "Launch Debugger",
 63    // if your project has multiple entry points, specify the one to use:
 64    // "mainClass": "com.myorganization.myproject.MyMainClass",
 65    //
 66    // this effectively sets a breakpoint at your program entry:
 67    "stopOnEntry": true,
 68    // the working directory for the debug process
 69    "cwd": "$ZED_WORKTREE_ROOT",
 70  },
 71]
 72```
 73
 74You should then be able to start a new Debug Session with the "Launch Debugger" scenario from the debug menu.
 75
 76## Launch Scripts (aka Tasks) in Windows
 77
 78This extension provides tasks for running your application and tests from within Zed via little play buttons next to tests/entry points. However, due to current limitations of Zed's extension interface, we can not provide scripts that will work across Maven and Gradle on both Windows and Unix-compatible systems, so out of the box the launch scripts only work on Mac and Linux.
 79
 80There is a fairly straightforward fix that you can apply to make it work on Windows by supplying your own task scripts. Please see [this Issue](https://github.com/zed-extensions/java/issues/94) for information on how to do that and read the [Tasks section in Zeds documentation](https://zed.dev/docs/tasks) for more information.
 81
 82## Advanced Configuration/JDTLS initialization Options
 83
 84JDTLS provides many configuration options that can be passed via the `initialize` LSP-request. The extension will pass the JSON-object from `lsp.jdtls.settings.initialization_options` in your settings on to JDTLS. Please refer to the [JDTLS Configuration Wiki Page](https://github.com/eclipse-jdtls/eclipse.jdt.ls/wiki/Running-the-JAVA-LS-server-from-the-command-line#initialize-request) for the available options and values. Below is an example `settings.json` that would pass on the example configuration from the above wiki page to JDTLS:
 85
 86```jsonc
 87{
 88  "lsp": {
 89    "jdtls": {
 90      "settings": {
 91        // this will be sent to JDTLS as initializationOptions:
 92        "initialization_options": {
 93          "bundles": [],
 94          // use this if your zed project root folder is not the same as the java project root:
 95          "workspaceFolders": ["file:///home/snjeza/Project"],
 96          "settings": {
 97            "java": {
 98              "home": "/usr/local/jdk-9.0.1",
 99              "errors": {
100                "incompleteClasspath": {
101                  "severity": "warning",
102                },
103              },
104              "configuration": {
105                "updateBuildConfiguration": "interactive",
106                "maven": {
107                  "userSettings": null,
108                },
109              },
110              "import": {
111                "gradle": {
112                  "enabled": true,
113                },
114                "maven": {
115                  "enabled": true,
116                },
117                "exclusions": [
118                  "**/node_modules/**",
119                  "**/.metadata/**",
120                  "**/archetype-resources/**",
121                  "**/META-INF/maven/**",
122                  "/**/test/**",
123                ],
124              },
125              "referencesCodeLens": {
126                "enabled": false,
127              },
128              "signatureHelp": {
129                "enabled": false,
130              },
131              "implementationCodeLens": "all",
132              "format": {
133                "enabled": true,
134              },
135              "saveActions": {
136                "organizeImports": false,
137              },
138              "contentProvider": {
139                "preferred": null,
140              },
141              "autobuild": {
142                "enabled": false,
143              },
144              "completion": {
145                "favoriteStaticMembers": [
146                  "org.junit.Assert.*",
147                  "org.junit.Assume.*",
148                  "org.junit.jupiter.api.Assertions.*",
149                  "org.junit.jupiter.api.Assumptions.*",
150                  "org.junit.jupiter.api.DynamicContainer.*",
151                  "org.junit.jupiter.api.DynamicTest.*",
152                ],
153                "importOrder": ["java", "javax", "com", "org"],
154              },
155            },
156          },
157        },
158      },
159    },
160  },
161}
162```
163
164## See also
165
166[Zed Java Repo](https://github.com/zed-extensions/java)
167[Eclipse JDTLS Repo](https://github.com/eclipse-jdtls/eclipse.jdt.ls)