Issue
I am trying to interoperate Scala with Java, and have managed to create a Maven based project (repository here) that compiles fine from commandline (mvn clean package
).
However, I keep running into the issue that my IDE (VS Code) doesn't understand the interoperation at all.
The Problems
The file AccessScala.java
is trying to access a variable in WithinModule.scala
.
package me.parent.jModule;
import me.parent.jModule.WithinModule;
public class AccessScala {
public static void main(String[] args) {
WithinModule within = new WithinModule();
// -- this is like a static variable, but Scala doesn't allow that
// System.out.println(WithinModule.string());
// -- this is the auto-generated getter
System.out.println(within.string());
// -- this getter was manually added
System.out.println(within.getString());
}
}
VS Code raises three problems:
- The import
me.parent.jModule.WithinModule
cannot be resolved :AccessScala[3,8]
WithinModule
cannot be resolved to a type :AccessScala [7,9]
WithinModule
cannot be resolved to a type :AccessScala [7,35]
Project Setup
The project has two modules, but right now only jModule
is relevant. it contains Java and Scala code in separate folders. sModule
contains some Scala code that I wanted to access as well, but my current problem has nothing to do with Maven modules and everything with VS Code understanding the Scala-Java connections.
> parent
| > jModule
| | > src/main
| | | > java/me/parent/jModule
| | | | > AccessScala.java
| | | > scala/me/parent/jModule
| | | | > WithinModule.scala
| | > build.sbt
| | > project/build.properties
| | > pom.xml
| > sModule
| | > src/scala/me/parent/sModule
| | | >ExternalModule.scala
| | > pom.xml
| | > build.sbt
| | > project/build.properties
| > build.sbt
| > project/build.properties
| > pom.xml
VS Code
I'm using the Java Extention Pack, Scala-Metals and Scala Syntax
Both for Java and Scala it detects errors etc, so it knows Scala is there.
Question
Why does VS Code complain and what could I do to make this work? Is there a way at all, or do I just have to use IntelliJ (i have other problems there, stay tuned for those questions ;)
Solution
Both Java as well as Scala language support for Visual Studio Code is based on LSP (Language Server Protocol), which doesn't allow currently an easy way to make the different language server work together yet.
Metals is currently able to compile both Scala and Java via Bloop (build server) or via build tools such as Sbt or Mill. However, it lacks most of the support needed for Java support. The Java LSP server cannot compile Scala at all currently, so using it in a mixed project is not recommended.
We are getting close to having some more Java support in Metals, please watch this PR or this isue for details, and we plan on working on it some more.
If you need some serious refactorings and automation it's recommended to use Intellij Idea currently. Metals is supposed to be a more lightweight alternative and also supports a range of different editors such as Vim, Emacs or Sublime. It was never intended to be a one to one experience, but to give users an alternative.
Answered By - Tomek Godzik