[[https://github.com/yveszoundi/eglot-java/blob/main/LICENSE][file:http://img.shields.io/badge/license-GNU%20GPLv3-blue.svg]] [[https://melpa.org/#/eglot-java][file:https://melpa.org/packages/eglot-java-badge.svg]] [[https://stable.melpa.org/#/eglot-java][file:https://stable.melpa.org/packages/eglot-java-badge.svg]]
All the bugs reports are welcome and appreciated.
Please note that the best way to contribute is via pull requests.
I may not notice immediately when something is broken, as I don't really program professionally anymore.
Overview
This package provides additional Java programming language support for [[https://github.com/joaotavora/eglot][eglot]].
|------------------------------------+-----------------------------------------------------------------------------------------+-----------| | Function | Description | Auto-load | |------------------------------------+-----------------------------------------------------------------------------------------+-----------| | =eglot-java-project-new= | Create new projects with a wizard ([[https://start.spring.io/][Spring]], [[https://micronaut.io/][Micronaut]], [[https://quarkus.io/][Quarkus]], [[https://vertx.io/][Vert.x]], [[https://maven.apache.org/][Maven]] or [[https://gradle.org/][Gradle]]) | YES | | =eglot-java-project-build-refresh= | Rebuild the current project | NO | | =eglot-java-project-build-task= | Run a build task using [[https://gradle.org/][Gradle]] or [[https://maven.apache.org/][Maven]] for the current project | NO | | =eglot-java-file-new= | Wizard for creating new Java files | NO | | =eglot-java-run-main= | Run the current main class | NO | | =eglot-java-run-test= | Run the current test ([[https://junit.org/junit5/][JUnit]] only, unless you run Maven or Gradle tasks) | NO | | =eglot-java-upgrade-lsp-server= | Upgrade the LSP server installation | YES | | =eglot-java-upgrade-junit-jar= | Upgrade the JUnit jar installation | YES | |------------------------------------+-----------------------------------------------------------------------------------------+-----------|
Note: eglot-java dynamically modifies the eglot-server-programs variable, you can change that behavior with the variable eglot-java-eglot-server-programs-manual-updates (See https://github.com/yveszoundi/eglot-java/issues/44).
You may prefer using the eglot defaults (=jdtls= Python script), =eglot-java= doesn't use that
=eglot-java= calls the relevant Java command directly, both for historical reasons and for potentially avoiding any Python dependency (Windows, Mac OS)
Dependencies
The only direct Emacs package dependency is [[https://github.com/joaotavora/eglot][eglot]], assuming you're running a recent Emacs version.
The [[https://projects.eclipse.org/projects/eclipse.jdt.ls/downloads][Eclipse JDT Language server]] along with the [[https://mvnrepository.com/artifact/org.junit.platform/junit-platform-console-standalone][JUnit console runner]] are configured to be automatically installed when not found at default locations (see =M-x customize-group= /eglot-java/).
Please also configure your [[https://www.java.com/en/download/help/path.html][PATH]] environment variable [[https://www.tutorialspoint.com/maven/maven_environment_setup.htm][for Maven]] and/or [[https://docs.gradle.org/current/userguide/installation.html][for Gradle]]. =Gradle= support isn't that mature compared to =Maven= overall (Maven has been around much longer...).
Installation
The =eglot-java= package is available on [[https://melpa.org/#/getting-started][MELPA]].
Once the =MELPA= repository is configured, please run =M-x package-install= and type =eglot-java=.
** Customization
You can configure few settings to reflect your preferences via =M-x customize-group= (/eglot-java/):
** Configuration
*** Initialization
Below is a sample minimal configuration, without any fancy library such as =use-package= or similar.
(add-hook 'java-mode-hook 'eglot-java-mode) (with-eval-after-load 'eglot-java (define-key eglot-java-mode-map (kbd "C-c l n") #'eglot-java-file-new) (define-key eglot-java-mode-map (kbd "C-c l x") #'eglot-java-run-main) (define-key eglot-java-mode-map (kbd "C-c l t") #'eglot-java-run-test) (define-key eglot-java-mode-map (kbd "C-c l N") #'eglot-java-project-new) (define-key eglot-java-mode-map (kbd "C-c l T") #'eglot-java-project-build-task) (define-key eglot-java-mode-map (kbd "C-c l R") #'eglot-java-project-build-refresh))
** LSP server startup errors
Usually this is due to starting the =LSP= server with an old Java version (see issue [[https://github.com/yveszoundi/eglot-java/issues/29][#29]]).
** Intermittent eglot timeout errors
You might want to set the value of =eglot-sync-connect= or =eglot-connect-timeout=.
** Gradle projects
If you have issues with [[https://gradle.org/][Gradle projects]] (code completion not working), then it's likely due to [[https://docs.gradle.org/current/userguide/compatibility.html][version incompatibilities]] (JDK and bundled Gradle Eclipse versions):
** Class file navigation
The =classFileContentsSupport= capability is registered with some known limitations. After visiting an initial "class contents buffer", further type definition navigation is not supported. This can be mitigated by the following workflow:
** LSP server upgrades
In earlier versions of =eglot-java=, the LSP server installation was reflecting the latest available snapshot.
As of =eglot-java 1.11= (December 2023), only milestones releases will be installed in order to mitigate challenges with buggy snapshot versions (See issues [[https://github.com/yveszoundi/eglot-java/issues/15][#15]] and [[https://github.com/yveszoundi/eglot-java/issues/16][#16]] for reference).
** LSP server initialization options
Sometimes you may want to add/modify [[https://github.com/eclipse-jdtls/eclipse.jdt.ls/wiki/Running-the-JAVA-LS-server-from-the-command-line#initialize-request][LSP server initialization settings]]. There are tons of them...
In the example below, the Google style of formatting is configured for later invocation via =M-x eglot-format=.
(setq eglot-java-user-init-opts-fn 'custom-eglot-java-init-opts) (defun custom-eglot-java-init-opts (server eglot-java-eclipse-jdt) "Custom options that will be merged with any default settings." '(:settings (:java (:format (:settings (:url "https://raw.githubusercontent.com/google/styleguide/gh-pages/eclipse-java-google-style.xml") :enabled t)))))
The list of LSP server settings is available in the [[https://github.com/eclipse-jdtls/eclipse.jdt.ls/wiki][Eclipse JDT.LS wiki]] on GitHub:
Per general Eclipse JDT.LS documentation, a basic skeleton of an initialization customization could look as follow:
'(:bundles: ["/home/me/.emacs.d/lsp-bundles/com.microsoft.java.debug.plugin-0.50.0.jar"] :workspaceFolders: ["file:///home/me/Projects/mavenproject"] :settings: (:java (:home "/usr/local/jdk21")) :extendedClientCapabilities (:classFileContentsSupport t))
** Debugging support
Please first setup the LSP =:bundles= in custom LSP initializing settings (per previous example)
** Systems such as NixOS or Gnu Guix
While =eglot-java= offers few auto-configuration settings for user convenience, this can be challenging for [[https://nixos.org/][NixOS]] or [[https://guix.gnu.org/][Gnu Guix]] users.
There are couple of variables that you can customize (=M-x customize-variable=):
Please also see [[https://github.com/yveszoundi/eglot-java/issues/46#issuecomment-2016032963][issue #46]].