Table of Contents

Java layer

Table of ContentsClose

java.png

1. Description

This layer adds support for the Java language.

1.1. Features:

  • Support for the following backends:
  • Auto-completion using company
  • Linting using flycheck integration
  • Maven integration
  • Interactive debugger using dap-mode

2. Layer Installation

2.1. Layer

To use this configuration layer, add it to your ~/.spacemacs. You will need to add java to the existing dotspacemacs-configuration-layers list in this file.

2.2. Choosing a backend

To choose a default backend set the layer variable java-backend:

(java :variables java-backend 'meghanada)

Alternatively the lsp backend will be automatically chosen if the layer lsp is used and you did not specify any value for java-backend.

Backend can be chosen on a per project basis using directory local variables (files named .dir-locals.el at the root of a project), an example to use the lsp backend:

;;; Directory Local Variables
;;; For more information see (info "(emacs) Directory Variables")

((java-mode (java-backend . lsp)))

Note: you can easily add a directory local variable with SPC f v d.

3. Backends

3.1. LSP Java

LSP Java is the Java adapter for LSP Mode which is the Emacs client for Language Server Protocol.

It integrates with company and flycheck. In addition it also provides full graphical debugging support via DAP mode.

LSP is the default backend used by this layer, see Choosing a backend in order to learn how to select a different backend.

3.1.1. Installation

The lsp server will be installed automatically whenever a Java file is opened.

3.2. Meghanada

Meghanada is composed of a lightweight server and an Emacs minor mode. It is unintrusive and integrates with company and flycheck.

Both Gradle and Maven projects are compatible with Meghanada.

3.2.1. Installation

The Meghanada server will be installed automatically whenever a Java file is opened.

If you get issues with the auto-installation you can either download the last version of the server manually or build from source hosted on the GitHub repository and copy the jar file to .emacs.d/.cache/meghanada folder.

No additional configuration is necessary!

3.2.2. About Maven

Maven is not fully supported by Meghanada (but should still work fine for most projects). If you encounter disconnection from the server then run mvn install at the root of your project first.

4. Key bindings

4.1. LSP Java

4.1.1. Code completion

Key binding Description
SPC m = Format code
SPC m c c Build project
SPC m c p Create spring boot project
SPC m e a Execute code action
SPC m e l List project errors/warnings
SPC m g A Search type in project using lsp-ui
SPC m g R Peek references using lsp-ui
SPC m g a Search type in project
SPC m g d Goto type definition
SPC m g g Go to definition
SPC m g k k Show type hierarchy
SPC m g k s Show sub type hierarchy
SPC m g k u Show super type hierarchy
SPC m g r Find references
SPC m h h Describe thing at point
SPC m q r Restart workspace
SPC m r a F Assign statement to field
SPC m r a a Assign all parameters to fields
SPC m r a f Assign parameter to field
SPC m r a i Add import
SPC m r a l Assign statement to local
SPC m r a m Add unimplemented methods
SPC m r a t Add throws exception
SPC m r c f Create field
SPC m r c i Convert to static import
SPC m r c p Create parameter
SPC m r e c Extract constant
SPC m r e l Extract local
SPC m r e m Extract method
SPC m r g e Generate equals and hashCode methods
SPC m r g g Generate getters/setters
SPC m r g o Generate method overrides
SPC m r g t Generate toString method
SPC m r o i Organize imports
SPC m r r Rename symbol
SPC m t b test browser
SPC m w u Refresh user settings

4.1.2. Debugger

Using the dap layer you'll get access to all the DAP key bindings, see the complete list of key bindings on the dap layer description.

4.2. Meghanada

4.2.1. Server

Key binding Description
SPC m D c Connect to server
SPC m D d Disconnect from server
SPC m D i Install server (should be done automatically
SPC m D k Kill server
SPC m D l Clear server cache
SPC m D p Ping server
SPC m D r Restart server
SPC m D s Start server
SPC m D u Update server
SPC m D v Print version of the server
Key binding Description
SPC m g b Jump back to previous location
SPC m g g Jump to declaration

4.2.3. Compilation

Key binding Description
SPC m c b Compile file (buffer)
SPC m c c Compile project

4.2.4. Tests (junit)

Key binding Description
SPC m t a Run all tests
SPC m t c Run test class around point
SPC m t l Run last tests
SPC m t t Run test cause around point

4.2.5. Refactoring

Key binding Description
SPC m = Beautify code
SPC m r i Optimize imports
SPC m r I Import all
SPC m r n Create a new class, interface, or enum

4.2.6. Tasks

Key binding Description
SPC m x : Run task

4.3. Maven

Key binding Description
SPC m m c c Compile
SPC m m c C Clean
SPC m m c r Clean and compile
SPC m m g a Switch between class and test file
SPC m m g A Switch between class and test file in another window
SPC m m t a Run all tests
SPC m m t C-a Clean and run all tests
SPC m m t b Run current buffer tests
SPC m m t i Test and install
SPC m m t t Run a specific test

Author: root

Created: 2024-04-03 Wed 19:39

Validate