Skip to content

Conversation

ibbem
Copy link

@ibbem ibbem commented Sep 5, 2025

Do not check for the existence of a JAR if a native command is preferred and there is a suitable executable.

I was hit by this issue on NixOS which puts the JAR at a different location than the auto detection of this package expects it. The alternative, setting lsp-java-server-install-dir such that the JAR instead of the executable is found, leads to a failure. When the JAR is launched directly, jdtls tries to write to the Nix Store which is read-only. Launching the jdtls executable provided by nixpkgs works perfectly though.

The error I encountered was exactly the same as described in #421. Hence, I believe this PR fixes that issue without overriding any private functions in the user configuration (see below).
As I understand it, this is also the alternative solution for #487 as suggested by @LuigiPiucco in #487 (comment)

In case this is desired, I could also add a default fallback in lsp-java--locate-server-command if lsp-java-jdt-ls-prefer-native-command is t and lsp-java-jdt-ls-command is found in exec-path.

Working NixOS configuration

When installing jdtls globally, my working configuration after this PR looks like this:

(setq lsp-java-jdt-ls-prefer-native-command t)
(setq lsp-java-server-install-dir (f-dirname (f-dirname (executable-find lsp-java-jdt-ls-command))))

Note that jdt-language-server was renamed to jdtls in nixpkgs. In case you are using some old nixpkgs version, you also need

(setq lsp-java-jdt-ls-command "jdt-language-server")

before the first two lines.

If you want to use nix shells with direnv, you can follow the guide by @dschrempf and use the following Emacs configuration:

(defun my-set-lsp-path ()
  (let ((jdtls-exe (executable-find lsp-java-jdt-ls-command)))
    (when jdtls-exe
      (setq lsp-java-jdt-ls-prefer-native-command t)
      (setq lsp-java-server-install-dir (f-dirname (f-dirname jdtls-exe))))))
(after! cc-mode
  (add-hook 'java-mode-hook #'my-set-lsp-path))

Do not check for the existence of a JAR if a native command is preferred
and there is a suitable executable.

I was hit by this issue on NixOS which puts the JAR at a different
location than the auto detection of this package expects it. The
alternative, setting `lsp-java-server-install-dir` such that the JAR
instead of the executable is found, leads to a failure. When the JAR is
launched directly, jdtls tries to write to the Nix Store which is
read-only. Launching the jdtls executable provided by nixpkgs works
perfectly though.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant