Emacs as Java IDE

Hook
This article describes how emacs can be configured as java ide. This article describes the use of GRADLE to configure JDTLS ; the most popular Java Language Server. Gradle will make use of eclipse plugin to generate .classpath and .project which is understood by JDTLS to configure project settings.
NOTE : Explore formatting and indenting
TODO: verify is annotation processing works.
Features
Code Completion
Error checking with flymake and eglot
I am a avid emacs user. I got introduced to Emacs when I was exploring lightweight editors which i could run in my 8 GB ram system. Traditional Editors did not make a cut for me because either they were niche to a language or had drastically different keybindings or both. Following towards ideas of minimalism, mouseless navigation and editors with zen look with tasteful themes I stumbled upon emacs.
I have been using more and more emacs to manage many things in my life, notes, raw editing, documentation and programming. While I use emacs with evil mode with a hybrid of emacs and evil keybinding for tasks other than editing text. I found i am quite at unease when i am outside my comfy editor.
Most of the programming i have done in emacs is with python and clojure. In the recent times i was working on java (again). So below is the documentation of how I have configured java in emacs with help of gradle and eclipse-gradle plugin
EGLOT with emacs
For language Server protocol support I use eglot. This comes by default in emacs 29. The language server command inside java-mode is jdtls. Which means it will use python executable of jdtls. For my regular spring boot project this default config gives library not found errors with host of other errors that signify that the proper configuration of lsp is missing.
Gradle Eclipse Plugin
Without this plugin, classpath configuration would require me to fiddle with jdtls run arguments.
To integrate eclipse plugin in gradle, add id “eclipse”
in the build.gradle
plugins {
id 'java-library'
id 'maven-publish'
id "org.springframework.boot" version "3.3.4"
id "eclipse"
}
repositories {
mavenLocal()
maven {
url = uri('https://repo.maven.apache.org/maven2/')
}
}
...
To make jdtls work we would need 2 files .classpath
and .project
.
Jdtls uses .classpath
to look for all the sources that it needs to build a project ; this includes all the source files and other libraries
.project
contains project specific settings like path of extra resources etc.
So if we want jdtls to take in similar settings while building the project as gradle, we simply have to generate proper .project
and .classpath
files.
First make backup of existing .project
and .classpath
files and remove these files if they exist in the project root directory.
To generate .classpath
file
./gradlew eclipse eclipseClassPath
To generate .project
file
./gradlew eclipse eclipseProject
And that is all, restart the eglot
server and you will see all the extra compilation errors and errors about unfounded libraries go away.
Subscribe to my newsletter
Read articles from Himanshu Tripathi directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
