5

If you follow the steps at the official Scala 3 sites, like Dotty or Scala Lang then it recommends using Coursier to install Scala 3. The problem is that neither or these explain how to run a compiled Scala 3 application after following the steps.

Scala 2:

> cs install scala
> scalac HelloScala2.scala
> scala HelloScala2
Hello, Scala 2!

Scala 3:

> cs install scala3-compiler
> scala3-compiler HelloScala3.scala

Now how do you run the compiled application with Scala 3?

Jack
  • 16,506
  • 19
  • 100
  • 167

3 Answers3

4

Currently there does not seem to be a way to launch a runner for Scala 3 using coursier, see this issue. As a workaround, you can install the binaries from the github release page. Scroll all the way down passed the contribution list to see the .zip file and download and unpack it to some local folder. Then put the unpacked bin directory on your path. After a restart you will get the scala command (and scalac etc) in terminal.

Another workaround is using the java runner directly with a classpath from coursier by this command:

java -cp $(cs fetch -p org.scala-lang:scala3-library_3:3.0.0):. myMain

Replace myMain with the name of your @main def function. If it is in a package myPack you need to say myPack.myMain (as usual).

Dale Wijnand
  • 6,054
  • 5
  • 28
  • 55
  • Thanks for the workaround, much appreciated. I'll keep this question unanswered for now in the hope that there is a way to get it set up with Coursier. – Jack Jun 23 '21 at 10:58
  • Thanks. I just updated the answer, making it clear that it is a workaround. – Bjorn Regnell Jun 23 '21 at 11:04
  • Just added another workaround that actually uses coursier, as asked for in the question. – Bjorn Regnell Jun 23 '21 at 11:30
  • @Jack This alternative has fewer dependencies but is longer to write: `java -cp $(cs fetch -p org.scala-lang:scala3-library_3:3.0.0):. run` Which workaround do you think is best? – Bjorn Regnell Jun 23 '21 at 12:19
  • 1
    Thanks Bjorn, the second option is probably the better option in this instance because the environment is still managed by coursier. Thanks for the help. – Jack Jun 23 '21 at 12:35
  • Another issue with using the classpath for the repl might be that a lot of unnecessary stuff is put on the classpath. So with that in mind, do you think it would be better to instead use the longer version with just the minimal needed stuff on -cp? Or is the shorter one preferable for its brevity? And a bonus/downside with the longer one is that you can/need to set the exact Scala version explicitly... – Bjorn Regnell Jun 23 '21 at 13:26
  • Sorry didn't see the last collapsed comment until after I'd edited the solution above. I think the scala3-library one is better, but I expect soon we'll have a proper "scala3" coursier app that sort this out properly and avoid the workaround. – Dale Wijnand Jul 20 '21 at 17:21
  • Ok - many thanks for your work on scala runners @DaleWijnand - it will be great when a proper scala3 solution is in place! – Bjorn Regnell Jul 21 '21 at 18:15
2

Finally, it seems that is possible to run scala application like scala 2 version using scala3 in Coursier:

cs install scala3

Then, you can compile it with scala3-compiler and run with scala3:

scala3-compiler Main.scala
scala3 Main.scala
gianluca aguzzi
  • 1,734
  • 1
  • 10
  • 22
1

This work-around seems to work for me:

cs launch scala3-repl:3+ -M dotty.tools.MainGenericRunner -- YourScala3File.scala

This way, you don't even have to compile the source code first.

In case your source depends on third-party libraries, you can specify the dependencies like this:

cs launch scala3-repl:3+ -M dotty.tools.MainGenericRunner -- -classpath \
    $(cs fetch --classpath io.circe:circe-generic_3:0.14.1):. \
    YourScala3File.scala

This would be an example where you use the circe library that's compiled with Scala 3. You should be able to specify multiple third-party libraries with the fetch sub-command.

Jake
  • 1,518
  • 2
  • 14
  • 20