Scala import cannot be resolved in Java project
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
- Create a Java play project using sbt
- Import it in ScalaIDE
- Create a class that imports
scala.collection.Seq - Open the project in VSCode.
Logs : No errors in the logs.
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"/>
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
<clas
I do so. But vscode still can't resolve scala imports.
https://marketplace.visualstudio.com/items?itemName=scalameta.metals https://scalameta.org/metals/docs/editors/vscode/
So does it solve now?
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
Apparently using Metals extension works for project that has both java and scala.