vscode-java icon indicating copy to clipboard operation
vscode-java copied to clipboard

Scala import cannot be resolved in Java project

Open s0meRandomDev opened this issue 7 years ago • 6 comments

I am working on a Java Play 2.6 project and wanted to switch from eclipse (ScalaIDE) to VSCode (because I use VSCode for everything else and I largely prefer to work with it rather than eclipse).

In Eclipse, I have no problem importing a Scala source ( scala.collection.Seq ) but in VScode, opening the same Eclipse project and having no problem with all other external librairies, it cannot resolve the scala.collection.Seq import. (more precisely it's the scala.collection import ).

When compiling my project in the console using sbt, I have no problem. Also Eclipse is able to resolve that import. Maybe it has to do with the "Scala library container" exlipse plugins of ScalaIDE

Environment
  • Linux Ubuntu 16.04
  • JDK version: 1.8
  • Visual Studio Code version: 1.23.1
  • Java extension version: Language support for Java(TM) by Red Hat 0.26.0
Steps To Reproduce
  1. Create a Java play project using sbt
  2. Import it in ScalaIDE
  3. Create a class that imports scala.collection.Seq
  4. Open the project in VSCode.

Logs : No errors in the logs.

s0meRandomDev avatar Jun 08 '18 11:06 s0meRandomDev

Ok so I solved my problem. It's not because of VS Code. For some reason the .classpath file is omitting two JARs that seem to be the "scala Library container" . When I manually add them the the .classpath file, it works.

I don't find this solution very clean though, so If anybody has a better suggestion, i'm all ears.

Lines added to the .classpath file :

<classpathentry kind="lib" path="/home/{username}/.eclipse/360744385_linux_gtk_x86_64/plugins/org.scala-lang.scala-library_2.12.3.v20170725-052526-VFINAL-6ac6da8.jar" sourcepath="/home/{username}/.eclipse/360744385_linux_gtk_x86_64/plugins/org.scala-lang.scala-library.source_2.12.3.v20170725-052526-VFINAL-6ac6da8.jar"/>
<classpathentry kind="lib" path="/home/{username}/.eclipse/360744385_linux_gtk_x86_64/plugins/org.scala-lang.scala-reflect_2.12.3.v20170725-052526-VFINAL-6ac6da8.jar" sourcepath="/home/{username}/.eclipse/360744385_linux_gtk_x86_64/plugins/org.scala-lang.scala-reflect.source_2.12.3.v20170725-052526-VFINAL-6ac6da8.jar"/>

s0meRandomDev avatar Jun 08 '18 13:06 s0meRandomDev

Sorry but vscode-java doesn't support scala:

  • As you guessed, Scala IDE adds this "Scala library container" which is not available in vscode-java (or rather, jdt.ls).
  • the scala files would not compile automatically without Scala IDE's Scala builder.
  • sbt's not supported at all.

I'm not a Scala user so I don't have the answers. Maybe switching to Maven would help (probably not), but right now, tweaking the .classpath is your best option

fbricon avatar Jun 08 '18 14:06 fbricon

<clas

I do so. But vscode still can't resolve scala imports.

JYInMyHeart avatar Aug 13 '21 09:08 JYInMyHeart

https://marketplace.visualstudio.com/items?itemName=scalameta.metals https://scalameta.org/metals/docs/editors/vscode/

GavinRay97 avatar Aug 13 '21 14:08 GavinRay97

So does it solve now?

Sigma711 avatar Sep 02 '24 07:09 Sigma711

has the same issue BTW: there is post on stack overflow: https://stackoverflow.com/questions/60189566/vs-code-knows-scala-and-java-but-cant-connect-them

jonefeewang avatar Oct 10 '24 09:10 jonefeewang

Apparently using Metals extension works for project that has both java and scala.

tambonbon avatar Nov 22 '24 11:11 tambonbon