Java layer

img/java.png

Description

This layer adds support for the Java language.

Features:

  • Support for the following backends:

  • Each provides:

    • Auto-completion using company
    • Linting using flycheck integration
    • Maven and Gradle integration

Layer Installation

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.

Choosing a backend

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

(java :variables java-backend 'eclim)

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 eclim backend:

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

((java-mode (java-backend . eclim)))

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

If you want to use the Ensime backend, you should modify your ~/.spacemacs to use the recommended Ensime version (Stable). Please add the following lines to dotspacemacs/user-init:

  (push '("melpa-stable" . "stable.melpa.org/packages/") configuration-layer-elpa-archives)
  (push '(ensime . "melpa-stable") package-pinned-packages)

Backends

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.

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

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 there 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!

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.

Eclim

Eclim provides the ability to access Eclipse features such as code completion, searching, code validation, and many more.

Installation

Eclipse

First you need the last version of Eclispe from the official Eclipse site.

Note: if you are on MacOS there some issues between the new native Eclipse application installed by the Eclispe-install.app wizard and the Eclim installer wizard. The recommended way to install Eclipse on MacOS is by using homebrew or by get the tar.gz compressed file instead. With homebrew the command line to install Eclipse is brew cask install eclipse-jee.

Eclim

For installation of Eclim check the official page to download the installer then double click on it or launch it in the command line with the following command (the example is for MacOS, replace the eclispe.home definition by your correct location as well as the current version of eclim):

$ java -Dvim.skip=true -Declipse.home=/Applications/Eclipse.app/Contents/Eclipse -jar eclim_2.6.0.jar install

Configuration

You may need to set the Eclipse and Eclim paths in your dotfile as layer variables or in the dotspacemacs/user-config, for instance on MacOS as layer variables:

(java :variables java-backend 'eclim
                eclim-eclipse-dirs '("/Applications/Eclipse.app/Contents/Eclipse/")
                eclim-executable "/Applications/Eclipse.app/Contents/Eclipse/eclim")

You can also configure useful other options like the default location for the Eclim workspace (default value is ~/workspace), as shown below:

  (setq
   ;; Specify the workspace to use by default
   eclimd-default-workspace "/path/to/default/eclipse/workspace"
   ;; wether autostarting eclimd or not (default nil)
   eclimd-autostart t
   ;; Whether or not to block emacs until eclimd is ready (default nil)
   eclimd-wait-for-process t)

Usage

Start the eclim daemon with SPC m D s and stop it with SPC m D k. (See below.)

Syntax checking is performed only on demand with SPC e e to preserve perfomance.

Auto-save feature may freeze Emacs sometimes, use C-g to unfreeze Emacs or atlernatively you can set dotspacemacs-auto-save-file-location to nil.

ENSIME

Another backend option is the ENSIME server, which is a lot more responsive than Eclim. ENSIME is originally made to work with Scala but it now works with plain Java projects as well.

Installation when using SBT

Find it with your favourite package manager, eg:

  nix-env --install sbt

or refer to the sbt installation instructions.

Installation when using Gradle

Refer to the Ensime installation instructions to install the ensime-gradle plugin.

Configuration

Follow the ENSIME configuration instructions. Spacemacs uses the development version of Ensime so follow the appropriate steps.

To use the build functions under SPC m b you need to use version 0.13.5 or newer of sbt, and specify that in your project's project/build.properties. For example,

  sbt.version=0.13.11

Usage

SPC SPC spacemacs/ensime-gen-and-restart or SPC m D r generates a new config for a project and starts the server. Afterwards SPC SPC ensime or SPC m D s will suffice do the trick.

Issues

ENSIME is originally built for Scala, so support for java is not complete, in particular refactoring doesn't work.

Key bindings

Meghanada

Server

Key BindingDescription
SPC m D cConnect to server
SPC m D dDisconnect from server
SPC m D iInstall server (should be done automatically
SPC m D kKill server
SPC m D lClear server cache
SPC m D pPing server
SPC m D rRestarrt server
SPC m D sStart server
SPC m D uUpdate server
SPC m D vPrint version of the server

Navigation

Key BindingDescription
SPC m g bJump back to previous location
SPC m g gJump to declaration

Compilation

Key BindingDescription
SPC m c bCompile file (buffer)
SPC m c cCompile project

Tests (junit)

Key BindingDescription
SPC m t aRun all tests
SPC m t cRun test class around point
SPC m t lRun last tests
SPC m t tRun test cause around point

Refactoring

Key BindingDescription
SPC m =Beautify code
SPC m r iOptimize imports
SPC m r IImport all
SPC m r nCreate a new class, interface, or enum

Tasks

Key BindingDescription
SPC m x :Run task

Eclim

Java-mode

Ant
Key BindingDescription
SPC m a aRun Ant
SPC m a cClear Ant cache
SPC m a rRun Ant
SPC m a vValidate
Project management
Key BindingDescription
SPC m p bBuild project
SPC m p cCreate project
SPC m p dDelete project
SPC m p gOpen file in current project
SPC m p iImport project
SPC m p jInformation about project
SPC m p kClose project
SPC m p oOpen project
SPC m p pOpen project management buffer
SPC m p rRun project
SPC m p uUpdate project
Eclim daemon
Key BindingDescription
SPC m D kKill daemon
SPC m D sStart daemon
Goto
Key BindingDescription
M-​,​jump back from go to declaration/definition
SPC m g ggo to declaration
SPC m g tgo to type definition
Refactoring
Key BindingDescription
SPC m r ccreate a constructor
SPC m r fformat file
SPC m r ggenerate getter and setter
SPC m r ioptimize imports
SPC m r jimplement or override a method
SPC m r ncreate a new class, enum, or interface
SPC m r rrename symbol
Documentation, Find
Key BindingDescription
SPC m f fgeneral find in project
SPC m h ccall hierarchy
SPC m h hshow documentation for symbol at point
SPC m h iview hierarchy
SPC m h ushow usages for symbol at point
Errors (problems)
Key BindingDescription
SPC e eForce an error check
SPC e lList errors
SPC e nNext error
SPC e pPrevious error
SPC m e eCorrect error around point
Tests
Key BindingDescription
SPC m t trun JUnit tests for current method or current file or project

Errors (problems) buffer

Key BindingDescription
RETgo to problem place
ashow all problems
eshow only errors
fshow problems only for current file
grefresh problems
qquit
wshow only warnings

Projects buffer

Key BindingDescription
RETgo to current project
cgo to problem place
Ddelete project
grefresh buffer
iinfo about current project
Iimport existing project into the workspace
mmark current project
Mmark all projects
Ncreate new project
oopen project
pupdate project
qquit
Rrename current project
uunmark current project
Uunmark all projects

Ensime key bindings

Search

Key BindingDescription
SPC m /incremental search using ensime-scalex major mode
SPC m ?incremental search in all live buffers

Ensime Search Mode

Key BindingDescription
C-jMove to next match
C-kMove to previous match
C-iInsert at point import of current result
RETJump to the target of the currently selected ensime-search-result
C-qQuit ensime search

sbt

Key BindingDescription
SPC m b .sbt transient state
SPC m b bsbt command
SPC m b ccompile
SPC m b Cclean command
SPC m b iswitch to sbt shell
SPC m b ppackage command
SPC m b rrun command

Typecheck

Key BindingDescription
SPC m c ttype check the current file
SPC m c Ttype check all the open buffers

Debug

Key BindingDescription
SPC m d AAttach to a remote debugger
SPC m d bset breakpoint
SPC m d Bclear breakpoint
SPC m d Cclear all breakpoints
SPC m d ccontinue
SPC m d iinspect value at point
SPC m d nnext
SPC m d ostep out
SPC m d qquit
SPC m d rrun
SPC m d sstep
SPC m d tbacktrace

Note: These key bindings need a transient-state, PR welcome :-)

Errors

Key BindingDescription
SPC m e eprint error at point
SPC m e lshow all errors and warnings
SPC m e sswitch to buffer containing the stack trace parser

Goto

Key BindingDescription
SPC m g ggo to definition
SPC m g igo to implementation
SPC m g tgo to test

Print and yank types

SPC m h Tprint full type name at point
SPC m h tprint short type name at point
SPC m y Tyank full type name at point
SPC m y tyank short type name at point

Documentation, Inspect

Key BindingDescription
SPC m h hshow documentation for symbol at point
SPC m h ushow uses for symbol at point
SPC m i iinspect type at point
SPC m i Iinspect type in other frame
SPC m i pinspect project package

Server

Key BindingDescription
SPC m D freload open files
SPC m D rregenerate the .ensime and restart the ensime server
SPC m D sstart ensime server

Refactoring

Key BindingDescription
SPC m r aadd type annotation
SPC m r fformat source
SPC m r dget rid of an intermediate variable (ensime-refactor-inline-local)
SPC m r Dget rid of an intermediate variable (ensime-undo-peek)
SPC m r iorganize imports
SPC m r mextract a range of code into a method
SPC m r rrename a symbol project wide
SPC m r timport type at point
SPC m r vextract a range of code into a variable
SPC m zexpand/contract region

Tests

Key BindingDescription
SPC m t atest command (sbt)
SPC m t rtest quick command (sbt)
SPC m t ttest only (sbt)

REPL

Key BindingDescription
SPC m s aask for a file to be loaded in the REPL
SPC m s bsend buffer to the REPL
SPC m s Bsend buffer to the REPL and focus the REPL buffer in insert state
SPC m s istart or switch to the REPL inferior process
SPC m s rsend region to the REPL
SPC m s Rsend region to the REPL and focus the REPL buffer in insert state

Maven

Key BindingDescription
SPC m m c cCompile
SPC m m c CClean
SPC m m c rClean and compile
SPC m m g aSwitch between class and test file
SPC m m g ASwitch between class and test file in another window
SPC m m t aRun all tests
SPC m m t C-aClean and run all tests
SPC m m t bRun current buffer tests
SPC m m t iTest and install
SPC m m t tRun a specific test

Gradle

Key BindingDescription
SPC m l c cCompile
SPC m l c CClean
SPC m l c rClean and compile
SPC m l t aRun all tests
SPC m l t bRun current buffer tests
SPC m l t tRun a specific test
SPC m l xExecute a Gradle task