:lang java

The poster child for carpal tunnel syndrome

1. Description

This module adds java support to Doom Emacs, including android-mode and groovy-mode.

1.1. Maintainers

This module has no dedicated maintainers. Become a maintainer?

1.2. Module flags

Enable integration with the eclim server.
Enable LSP support for java-mode, Requires :tools lsp and a langserver (supports eclipse.jdt.ls). Incompatible with +meghanada.
Enable meghanada-mode. Incompatible with +lsp.

1.4. Hacks

No hacks documented for this module.

2. Prerequisites

  • This module requires the Java SDK.
  • The lsp test runner requires that :tools debugger +lsp is enabled, as this provides dap-mode which contains the Java test runner.

2.1. OpenJDK 11

2.1.1. Ubuntu

$ apt-get install openjdk-11-jdk-headless

2.1.2. Fedora

$ dnf install java-11-openjdk

2.2. Oracle JDK 11

2.2.1. Ubuntu

$ add-apt-repository ppa:linuxuprising/java
$ apt update
$ apt install oracle-java11-installer
$ apt install oracle-java11-set-default

2.2.2. Fedora

$ curl -O https://download.java.net/java/GA/jdk11/9/GPL/openjdk-11.0.2_linux-x64_bin.tar.gz
$ tar zxvf openjdk-11.0.2_linux-x64_bin.tar.gz
$ sudo mv jdk-11.0.2/ /usr/local/

Then append the following to /etc/profile.d/jdk11.sh:

export JAVA_HOME=/usr/local/jdk-11.0.2
export PATH=$PATH:$JAVA_HOME/bin

Save the file and source it:

$ source /etc/profile.d/jdk11.sh
$ java -version

2.3. Multiple Java Versions

It is common to need support for multiple Java versions. You can use a generic tool like jabba to install and manage multiple Java versions on any OS.

To switch between Java versions in Doom, you can use direnv and :tools direnv. To set a Java version for a particular project, create a .envrc pointing to the Java installation in the root of the project:

PATH_add ~/.jabba/jdk/adopt@1.11.0-3

And then run $ direnv allow . in the project directory. If the direnv module is enabled, then Doom will automatically source this environment before executing the LSP server.

3. Usage

3.1. +lsp features

According to https://github.com/emacs-lsp/lsp-java it adds:

  • As you type reporting of parsing and compilation errors (via flycheck/lsp-ui)
  • Code completion - using company-lsp or builtin complete-at-point
  • Javadoc hovers - using lsp-ui
  • Code actions - using lsp-ui
  • Code outline - using builtin imenu
  • Code navigation - using builtin xref
  • Code lens (references/implementations) - using builtin xref
  • Highlights
  • Code formatting
  • Maven pom.xml project support
  • Limited Gradle support
  • Visual debugger - dap-mode
  • Test runner - dap-mode
  • Project explorer integration - treemacs
  • Integration with Spring Initializr

3.2. +meghanada features

According to https://github.com/mopemope/meghanada-emacs/ it adds:

  • Auto-update server module
  • Gradle and Maven and Eclipse project support
  • No need build tool’s plugin
  • Run build tool task
  • Compile your project
  • Syntax check and analyze java source (flycheck-meghanada)
  • Support Generic Types
  • Code completion with company-mode (company-meghanada)
  • Optimize import and sort
  • Jump declaration
  • Run JUnit test (include test runner)
  • Diagnostic reporting with flycheck (flycheck-meghanada)
  • Show symbol’s type info with el-doc
  • Search references
  • Full-featured text search

4. Configuration

4.1. +lsp

Install the eclipse server by executing M-x lsp-install-server and selecting jdtls. After that any newly opened java files should start the LSP server automatically.

To update the server, perform SPC u M-x lsp-install-server.

Note that if you change Java version you may need to remove the LSP server and install it again. You can do this with M-x +lsp/uninstall-server followed by M-x lsp-install-server.

Enable the :tools debugger +lsp module to get test runner support.

5. Troubleshooting

There are no known problems with this module. Report one?

6. TODO Appendix

This module has no appendix yet. Write one?