Eglot and Eclipse JDT for Java

Last time I mentioned that I helped making it possible for Eglot to use jdtls, the Python helper script packaged with Eclipse JDT to start the language server with sane defaults. The only problem I had during my work on this is that jdtls requires Python 3.9 or later, why my Ubuntu 20.04 machine comes with the 3.8.10 version. Since changing the default Python on Ubuntu can bring pain1, I decided to have Emacs work with Python 3.9 only for my Java projects.

First, I installed the required Python packages:

$ sudo apt install python3.9 python3-9-venv

Then, in my Java project root directory I ran:

$ python3.9 -m venv env

Finally, I had to tell Emacs how to interact with this new Python environment. In the root directory of my project I added a .dir-locals.el file with the following content:

((nil . ((buffer-env-command . ">&2 . \"$0\" && env -0")
         (buffer-env-script-name . "env/bin/activate")
         (python-shell-virtualenv-root . "./env"))))

I am using Augusto Stoffel’s buffer-env to pick up the activate script and run it automatically for me. Checking the Python version with M-! /usr/bin/env python3 -V RET told me I was doing the right thing. Keep in mind that you don’t have to set buffer-env-command like I do. Here I am setting it back to its default value because I changed it in my init.el to specifically deal with direnv. Anyway, with everything in place Eglot correctly interfaced with jdtls for a smooth Java developing experience.

A note to myself: I will probably need to revisit this setup as soon as I move to the new Ubuntu LTS which, I guess, will provide a more recent Python by default.


  1. Trust me on this.