using-java-in-emacs.org
Java layer
Description
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 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 | Restarrt server |
SPC m D s | Start server |
SPC m D u | Update server |
SPC m D v | Print version of the server |
Navigation
Key Binding | Description |
---|---|
SPC m g b | Jump back to previous location |
SPC m g g | Jump to declaration |
Compilation
Key Binding | Description |
---|---|
SPC m c b | Compile file (buffer) |
SPC m c c | Compile project |
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 |
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 |
Tasks
Key Binding | Description |
---|---|
SPC m x : | Run task |
Eclim
Java-mode
Ant
Key Binding | Description |
---|---|
SPC m a a | Run Ant |
SPC m a c | Clear Ant cache |
SPC m a r | Run Ant |
SPC m a v | Validate |
Project management
Key Binding | Description |
---|---|
SPC m p b | Build project |
SPC m p c | Create project |
SPC m p d | Delete project |
SPC m p g | Open file in current project |
SPC m p i | Import project |
SPC m p j | Information about project |
SPC m p k | Close project |
SPC m p o | Open project |
SPC m p p | Open project management buffer |
SPC m p r | Run project |
SPC m p u | Update project |
Eclim daemon
Key Binding | Description |
---|---|
SPC m D k | Kill daemon |
SPC m D s | Start daemon |
Goto
Key Binding | Description |
---|---|
M-, | jump back from go to declaration/definition |
SPC m g g | go to declaration |
SPC m g t | go to type definition |
Refactoring
Key Binding | Description |
---|---|
SPC m r c | create a constructor |
SPC m r f | format file |
SPC m r g | generate getter and setter |
SPC m r i | optimize imports |
SPC m r j | implement or override a method |
SPC m r n | create a new class, enum, or interface |
SPC m r r | rename symbol |
Documentation, Find
Key Binding | Description |
---|---|
SPC m f f | general find in project |
SPC m h c | call hierarchy |
SPC m h h | show documentation for symbol at point |
SPC m h i | view hierarchy |
SPC m h u | show usages for symbol at point |
Errors (problems)
Key Binding | Description |
---|---|
SPC e e | Force an error check |
SPC e l | List errors |
SPC e n | Next error |
SPC e p | Previous error |
SPC m e e | Correct error around point |
Tests
Key Binding | Description |
---|---|
SPC m t t | run JUnit tests for current method or current file or project |
Errors (problems) buffer
Key Binding | Description |
---|---|
RET | go to problem place |
a | show all problems |
e | show only errors |
f | show problems only for current file |
g | refresh problems |
q | quit |
w | show only warnings |
Projects buffer
Key Binding | Description |
---|---|
RET | go to current project |
c | go to problem place |
D | delete project |
g | refresh buffer |
i | info about current project |
I | import existing project into the workspace |
m | mark current project |
M | mark all projects |
N | create new project |
o | open project |
p | update project |
q | quit |
R | rename current project |
u | unmark current project |
U | unmark all projects |
Ensime key bindings
Search
Key Binding | Description |
---|---|
SPC m / | incremental search using ensime-scalex major mode |
SPC m ? | incremental search in all live buffers |
Ensime Search Mode
Key Binding | Description |
---|---|
C-j | Move to next match |
C-k | Move to previous match |
C-i | Insert at point import of current result |
RET | Jump to the target of the currently selected ensime-search-result |
C-q | Quit ensime search |
sbt
Key Binding | Description |
---|---|
SPC m b . | sbt transient state |
SPC m b b | sbt command |
SPC m b c | compile |
SPC m b C | clean command |
SPC m b i | switch to sbt shell |
SPC m b p | package command |
SPC m b r | run command |
Typecheck
Key Binding | Description |
---|---|
SPC m c t | type check the current file |
SPC m c T | type check all the open buffers |
Debug
Key Binding | Description |
---|---|
SPC m d A | Attach to a remote debugger |
SPC m d b | set breakpoint |
SPC m d B | clear breakpoint |
SPC m d C | clear all breakpoints |
SPC m d c | continue |
SPC m d i | inspect value at point |
SPC m d n | next |
SPC m d o | step out |
SPC m d q | quit |
SPC m d r | run |
SPC m d s | step |
SPC m d t | backtrace |
Note: These key bindings need a transient-state, PR welcome :-)
Errors
Key Binding | Description |
---|---|
SPC m e e | print error at point |
SPC m e l | show all errors and warnings |
SPC m e s | switch to buffer containing the stack trace parser |
Goto
Key Binding | Description |
---|---|
SPC m g g | go to definition |
SPC m g i | go to implementation |
SPC m g t | go to test |
Print and yank types
SPC m h T | print full type name at point |
SPC m h t | print short type name at point |
SPC m y T | yank full type name at point |
SPC m y t | yank short type name at point |
Documentation, Inspect
Key Binding | Description |
---|---|
SPC m h h | show documentation for symbol at point |
SPC m h u | show uses for symbol at point |
SPC m i i | inspect type at point |
SPC m i I | inspect type in other frame |
SPC m i p | inspect project package |
Server
Key Binding | Description |
---|---|
SPC m D f | reload open files |
SPC m D r | regenerate the .ensime and restart the ensime server |
SPC m D s | start ensime server |
Refactoring
Key Binding | Description |
---|---|
SPC m r a | add type annotation |
SPC m r f | format source |
SPC m r d | get rid of an intermediate variable (ensime-refactor-inline-local ) |
SPC m r D | get rid of an intermediate variable (ensime-undo-peek ) |
SPC m r i | organize imports |
SPC m r m | extract a range of code into a method |
SPC m r r | rename a symbol project wide |
SPC m r t | import type at point |
SPC m r v | extract a range of code into a variable |
SPC m z | expand/contract region |
Tests
Key Binding | Description |
---|---|
SPC m t a | test command (sbt) |
SPC m t r | test quick command (sbt) |
SPC m t t | test only (sbt) |
REPL
Key Binding | Description |
---|---|
SPC m s a | ask for a file to be loaded in the REPL |
SPC m s b | send buffer to the REPL |
SPC m s B | send buffer to the REPL and focus the REPL buffer in insert state |
SPC m s i | start or switch to the REPL inferior process |
SPC m s r | send region to the REPL |
SPC m s R | send region to the REPL and focus the REPL buffer in insert state |
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 |
Gradle
Key Binding | Description |
---|---|
SPC m l c c | Compile |
SPC m l c C | Clean |
SPC m l c r | Clean and compile |
SPC m l t a | Run all tests |
SPC m l t b | Run current buffer tests |
SPC m l t t | Run a specific test |
SPC m l x | Execute a Gradle task |