VS Code knows Scala and Java, but can't connect them
Asked Answered
C

1

7

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:

  1. The import me.parent.jModule.WithinModule cannot be resolved : AccessScala[3,8]
  2. WithinModule cannot be resolved to a type : AccessScala [7,9]
  3. 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 ;)

Callas answered 12/2, 2020 at 13:33 Comment(8)
@tgkprog There are essentially no similarities between VSCode and Eclipse.Innerdirected
Did you try the scale ide scala-ide.org Its a souped version of eclipse. Not sure how VS code works but did u try importing the maven project? That's how we do it in eclipse.Rollo
Main thing is: VSCode isn't really an IDE, it's a source code editor. For IDE to provide you interlanguage suggestions like this, it has to compile the code and extract suggestions from classfiles. VSCode doesn't compile anything.Housework
I started with Eclipse, but the scala plugin for it doesn't work for me at all, and the dedicated one, from what I can tell, doesn't support Java 11 (I believe it has been a while since it has been updated?).Callas
@M.Prokhorov so no way to make this work? I figured the point of it being an editor is so it can be extended with plugins, like [metals] so it can compile, run tests etc. that's how I get those error messages in the first placeCallas
If you're working with Scala/Java I recommend using a proper IDE like IntelliJ (Community edition is pretty decent if you don't want to pay for Ultimate).Hohenlinden
@SonkeWohler, if the plugin wraps a compiler binary, then it might work. I think I've seen java plugin that has Intellisense. Maybe if there's a scala plugin that does the same, then it would work. I'm not sure those plugin that you're using support what you're trying to do though. Try different plugins, or an actual IDE like other people suggested. As for Java 11 support in eclipse - try it. It's possible that it either mostly works, or works by adding couple --add-opens flags in command line.Housework
I'd recommend using IntelliJ for Scala work.Juttajutty
S
7

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.

Sue answered 28/9, 2021 at 13:53 Comment(3)
Hi, Tomek! Really appreciate the work you have done.Hartsock
I was playing around with some mixed scala/java code in an SBT build. I have been using metals (v1.20.0) and I really dig it. I have tried to add logback to the classpath, but the IDE simply cannot resolve imports from 3rd-party jars in the .java source. The java lanhuage server from Redhat is installed, so basic java syntax is fine. But imports are unresolved in .java files. Thoughts?Hartsock
We currently need source jars for resolving the exact position of a Java or Scala symbol, if they are published together with the 3rd party jars we should be able to find them. Without them we would need to decompile the classfile and use that for showing definition, which is not yet supported. As for java language server, there currently no way for them to interact with each other. Please do report an issue for the 3rd party jars, we can try and look into that.Sue

© 2022 - 2024 — McMap. All rights reserved.