1

I am trying to run Scala in VS Code, a simple hello world statement, but I get this output:

PS C:\Users\thmga\OneDrive\Documents\Notebook\vscode_test> scala "c:\Users\thmga\OneDrive\Documents\Notebook\vscode_test\test.scala"
Exception in thread "main" dotty.tools.scripting.StringDriverException: No main methods detected for [c:\Users\thmga\OneDrive\Documents\Notebook\vscode_test\test.scala]
        at dotty.tools.scripting.StringDriverException$.apply(StringDriver.scala:47)
        at dotty.tools.scripting.Util$.detectMainClassAndMethod(Util.scala:51)
        at dotty.tools.scripting.ScriptingDriver.compileAndRun(ScriptingDriver.scala:28)
        at dotty.tools.scripting.Main$.main(Main.scala:45)
        at dotty.tools.MainGenericRunner$.run$1(MainGenericRunner.scala:249)
        at dotty.tools.MainGenericRunner$.main(MainGenericRunner.scala:268)
        at dotty.tools.MainGenericRunner.main(MainGenericRunner.scala)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:568)
        at coursier.bootstrap.launcher.a.a(Unknown Source)
        at coursier.bootstrap.launcher.Launcher.main(Unknown Source)

The code:

@main def hello(): Unit =
  println("Hello, World!")

I tried with the file named "test.sc" and "test.scala". When I used "test.scala" the icon of Scala appeared beside the file.

I installed Scala Syntax (official) and Scala (Metals). Anyone any clue? Thanks.

  • Does the behavior change if you replace your code with https://scastie.scala-lang.org/DmytroMitin/fmCb0LZFSoWH1JTd2UDY0g/2 ? Your code is in Scala 3, mine is in Scala 2. If this does change the behavior then the thing is VSCode doesn't know that you're using Scala 3. If the behavior is the same then something else is not set up. – Dmytro Mitin Oct 30 '22 at 23:43
  • But `dotty.tools.scripting...` is from Scala 3. Weird. What JDK do you use? There are some reports about issues in recent JDKs like 11, 18, 19. [1](https://stackoverflow.com/questions/73975781/how-to-resolve-scalac-error-assertion-failed) [2](https://stackoverflow.com/questions/73988814/package-sun-awt-windows-does-not-exist) – Dmytro Mitin Oct 30 '22 at 23:58
  • Did you look at [Getting Started](https://docs.scala-lang.org/getting-started/index.html) and specifically [Using VSCode with metals](https://docs.scala-lang.org/getting-started/index.html#using-vscode-with-metals)? – Dmytro Mitin Oct 31 '22 at 00:02

0 Answers0