0

I have added the junit jar file in my ${CLASSPATH} environment variable, but when I use the lsp for neovim it doesn't recognize the functions and imports from the JAR file.

import static org.junit.Assert.assertEquals;     ■ The import org.junit cannot be resolved
  1 import static org.junit.Assert.assertTrue;     ■ The import org.junit cannot be resolved
  2 import static org.junit.Assert.fail;     ■ The import org.junit cannot be resolved
  3
  4 import java.util.Comparator;
  5 import java.util.Arrays;
  6 import java.util.List;
  7 import java.util.ArrayList;
  8
  9 import org.junit.Test;     ■ The import org.junit cannot be resolved
 10 import org.junit.Before;     ■ The import org.junit cannot be resolved

Does anyone know a way to add the ${CLASSPATH} to the imports and "stuff" that jdtls recognizes?

ujjain
  • 9
  • 1
  • 4

2 Answers2

0

One has to specify the correct plugin to initialize the (Eclipse)language server within the JDK. We can do this by creating a shell script that is called by your nvim LSP plugin/config.

First you have to locate the equinox_launcher jar file, which is located in the eclipse/jdt-language-server/plugin folder and add it to your script.

Linux shell script should look something like this:

#!/usr/bin/env/ bash
    
JAR="path/to/your/eclipse/build/plugins/org.eclipse.equinox.launcher_longnumberhere.jar" 
GRADLE_HOME=$HOME/where_gradle_lives/gradle $JAVA_HOME/or_absolute_path \/bin/java \
              -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=1044 \
              -Declipse.application=org.eclipse.jdt.ls.core.id1 \
              -Dosgi.bundles.defaultStartLevel=4 \
              -Declipse.product=org.eclipse.jdt.ls.core.product \
              -Dlog.level=ALL -noverify \
              -Xmx1G -jar echo $(JAR) \ // points to the plugin
              -configuration ./config_linux \ // points to the config folder within jdtls  
              -data /path/to/data \
              --add-modules=ALL-SYSTEM \
              --add-opens java.base/java.util=ALL-UNNAMED \
              --add-opens java.base/java.lang=ALL-UNNAMED 

Win batch script should look something like this:

SET "JAR=path\to\your\eclipse\build\plugins\org.eclipse.equinox.launcher_longnumberhere.jar"
SET "GRADLE_HOME=path\where\gradle\lives\gradle %JAVA_HOME%\or_absolute_path\bin\java"^
      -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=1044^
      -Declipse.application=org.eclipse.jdt.ls.core.id1^
      -Dosgi.bundles.defaultStartLevel=4^
      -Declipse.product=org.eclipse.jdt.ls.core.product^
      -Dlog.level=ALL^ 
      -noverify^
      -Xmx1G -jar echo %JAR%^ // this points to the plugin
      -configuration .\config_win^ // points to the config folder within jdtls  
      -data \path\to\data^ 
      --add-modules=ALL-SYSTEM^
      --add-opens java.base/java.util=ALL-UNNAMED^
      --add-opens java.base/java.lang=ALL-UNNAMED

After the agentlib line, you can add the modules you need. For further conveniences, the java_lsp.sh or java_lsp.bat can be included in a environment path.
The jdtls target directories can vary if you build from source, or (recommended) use a Milestone build.
https://download.eclipse.org/jdtls/milestones/

For more detailed information you can check out:

Neovim built in LSP documentation:

Guide + documentation regarding JDTLS:

Guide + Examples Regarding JDTLS client plugin for nvim written in lua:

Seabass
  • 82
  • 6
  • This doesn't seem to answer the question. This step has already been done, what they are asking is how to make jdtls recognize dependencies. – Emiliano Ruiz Feb 24 '22 at 21:10
  • "Does anyone know a way to add the ${CLASSPATH} to the imports and "stuff" that jdtls recognizes?" My answer clearly describes this. – Seabass Feb 26 '22 at 08:18
0

I recently went through the process of trying to get this to work (note that I used jdtls-launcher to run JDTLS, not sure if that affects whether my answer will work), and essentially found that you need to add a line to the .classpath file in the workspace directory that JDTLS automatically creates when you start a new project (located at ~/.cache/jdtls-workspace/my_project_name_with_numbers_after for me). According to section 3.4 of this documentation, the line should look like this:

<classpathentry kind="lib" path="/path/to/junit-platform-console-standalone-1.8.2.jar"/>

Where /path/to/junit-platform-console-standalone-1.8.2.jar is wherever you may have downloaded junit-platform-console-standalone-1.8.2.jar from here (or whatever version of JUnit you want).

However, I explain what I did to get it to work in more detail in this GitHub issue, so I'd suggest you check that out for more information.

Dharman
  • 30,962
  • 25
  • 85
  • 135