Home

Awesome

MELPA MELPA Stable Build Status Join the chat at https://gitter.im/emacs-lsp/lsp-mode

lsp-java

Emacs Java IDE using Eclipse JDT Language Server.

Requirements

Features

LSP java mode supports the following JDT Features:

Demo

Here it is a demo from EmacsConf2019 https://www.youtube.com/watch?v=Bbjxn9yVNJ8

Tutorial

Here is a tutorial that covers setup and use https://xpressrazor.wordpress.com/2020/11/04/java-programming-in-emacs/

Screenshot

demo

Installation

Spacemacs

lsp-java is included in spacemacs (for now only on the dev branch). If you are using the development version of spacemacs you can simply add (java :variables java-backend 'lsp) to dotspacemacs-configuration-layers.

Install via melpa

The recommended way to install LSP Java is via package.el - the built-in package manager in Emacs. LSP Java is available on the two major package.el community maintained repos - MELPA Stable and MELPA.

<kbd>M-x</kbd> package-install <kbd>[RET]</kbd> lsp-java <kbd>[RET]</kbd>

Then add the following lines to your .emacs and open a file from the any of the specified projects.

(require 'lsp-java)
(add-hook 'java-mode-hook #'lsp)

Eclipse JDT Language Server

lsp-java will automatically detect when the server is missing and it will download Eclipse JDT Language Server before the first startup. The server installation will be in lsp-java-server-install-dir. It will detect whether dap-mode is present and it will download the required server side plugins/components. If you want to update the server you can run lsp-java-update-server. To run specific version of Eclipse JDT Language Server use lsp-java-server-install-dir.

Quick start

Minimal configuration with company-capf and lsp-ui and dap-mode. Set lsp-java-workspace-dir in case you have existing Java projects. Now you can explore the methods under lsp-java-*, dap-java-*, dap-*, and lsp-*.

(condition-case nil
    (require 'use-package)
  (file-error
   (require 'package)
   (add-to-list 'package-archives '("melpa" . "http://melpa.org/packages/"))
   (package-initialize)
   (package-refresh-contents)
   (package-install 'use-package)
   (setq use-package-always-ensure t)
   (require 'use-package)))

(use-package projectile)
(use-package flycheck)
(use-package yasnippet :config (yas-global-mode))
(use-package lsp-mode :hook ((lsp-mode . lsp-enable-which-key-integration)))
(use-package hydra)
(use-package company)
(use-package lsp-ui)
(use-package which-key :config (which-key-mode))
(use-package lsp-java :config (add-hook 'java-mode-hook 'lsp))
(use-package dap-mode :after lsp-mode :config (dap-auto-configure-mode))
(use-package dap-java :ensure nil)
(use-package helm-lsp)
(use-package helm
  :config (helm-mode))
(use-package lsp-treemacs)

Supported commands

LSP Mode commands

LSP Java commands

Refactoring

LSP Java provides rich set of refactorings via Eclipse JDT Language Server code actions and some of them are bound to Emacs commands:

Testing support

Dependency viewer

STS4 Integration (experimental)

LSP java has integration with STS4 providing the following functionality.

Spring boot support (Experimental)

In addition to the integration with Eclipse JDT Language Server lsp-java provides integration with STS4 which covers Spring Boot application.properties, application.yml and .java files.

Usage:

Make sure that you have configured JAVA_HOME. lsp-java will automatically download the STS4 when you call lsp-java-update-server. In order to enable STS4 integration add the following lines to your config:

(require 'lsp-java-boot)

;; to enable the lenses
(add-hook 'lsp-mode-hook #'lsp-lens-mode)
(add-hook 'java-mode-hook #'lsp-java-boot-lens-mode)

Functionality for .java

Navigating the source code - Go to symbol in file/workspace

Easy navigation to Spring-specific elements of your source code.

Go to Symbol in workspace

Commands

lsp-workspace-symbol - (works better usign helm-lsp)

Examples

Quick-access for running apps

Easy navigation to the provided request mappings of running apps.

accessing running apps quickly

Commands

lsp-workspace-symbol - (works better usign helm-lsp)

Examples

Live application information hovers

STS4 automatically detects JVM processes for running boot applications on your local machine.

For some types of information, STS 4 may also show a 'quick summary' as a codelens.

If there are multiple instances of the app running on your machine, the live data from all those instances will show up in the hover information.

(add-hook 'java-mode-hook #'lsp-java-boot-lens-mode)

live data from running apps as hover on source code

Examples

Code templates

Write Spring code with templates, available via regular code completion.

Examples

Smart code completions

Additional code completions for Spring-specific annotations

Smart code completion for boot properties

Functionality for .properties and .yml

This extension analyzes your project's classpath and parses and indexes any Spring Boot Properties Metadata it finds. Both Maven and Gradle projects are supported.

The data in the index is used to provide validation, code completions and information hovers while editing Spring Boot Properties in either .properties or .yml format.

Validation and code completion in properties file

application-properties-validation

Validation and code completion in yaml file

application-properties-validation

Spring Initializr

lsp-java provides a frontend for Spring Initializr which simplifies the creation of Spring Boot projects directly from Emacs via lsp-java-spring-initializr. Create Spring boot project

Supported settings

Additional packages

FAQ

(setq lsp-inhibit-message t)
(setq lsp-ui-sideline-update-mode 'point)

When particular file is not part of imported project Eclipse JDT Language Server could not calculate the current classpath.

Have a look at ~/.emacs.d/workspace/.metadata/.plugins/org.eclipse.jdt.launching/libraryInfos.xml. If you updated your local java path and want LSP to use the new version, try removing the ~/.emacs.d/workspace/ directory and relaunch LSP. Also see #114. If you have multiple java JDK versions installed and want to change the version of java used by LSP, also need to set lsp-java-configuration-runtimes. An example for setting lsp-java-configuration-runtimes:

(setq lsp-java-configuration-runtimes '[(:name "JavaSE-1.8"
						:path "/home/kyoncho/jdk1.8.0_201.jdk/")
					(:name "JavaSE-11"
						:path "/home/kyoncho/jdk-11.0.1.jdk/"
						:default t)])
;; current VSCode defaults
(setq lsp-java-vmargs '("-XX:+UseParallelGC" "-XX:GCTimeRatio=4" "-XX:AdaptiveSizePolicyWeight=90" "-Dsun.zip.disableMemoryMapping=true" "-Xmx2G" "-Xms100m"))