Skip to content

Commit e7a4c7e

Browse files
committed
Explain how to specify the main class and add compiled classes to the classpath in the scala runner migration guide
1 parent 7975036 commit e7a4c7e

File tree

1 file changed

+71
-3
lines changed

1 file changed

+71
-3
lines changed

website/docs/guides/introduction/old-runner-migration.md

Lines changed: 71 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ If you merely want to get started with Scala CLI, you might want to first look
1919
at [the Getting started page](../../getting_started.md).
2020
:::
2121

22-
## How to test Scala CLI as the new `scala` command?
22+
## How to start using Scala CLI as the new `scala` command?
2323

24-
There is a dedicated `scala-experimental` distribution of Scala CLI, which can install it as `scala` on your machine.
25-
For instructions on how to try it out, refer to [the relevant doc](../../reference/scala-command/index.md).
24+
Refer to the [official instructions for installing Scala](https://www.scala-lang.org/download/).
25+
Scala CLI is available as the `scala` command alongside the Scala distribution in Scala 3.5.0 and later.
2626

2727
## How has the passing of arguments been changed from the old `scala` runner to Scala CLI?
2828

@@ -381,3 +381,71 @@ println("Args: " + args.mkString(" "))
381381

382382
For more information about the `shebang` sub-command, refer to [the appropriate doc](../../commands/shebang.md).
383383
For more details on how to use Scala CLI in shebang scripts, refer to [the relevant guide](../scripting/shebang.md).
384+
385+
## How to run a main class from compiled sources with Scala CLI?
386+
387+
With the old `scala` runner, running a main class from compiled sources was as simple as passing the main class name
388+
as an arg. The old runner would then assume the current working directory is to be added to the classpath and could
389+
implicitly run any compiled class files it would find.
390+
391+
```scala title=hello.scala
392+
@main def hello = println("Hello")
393+
```
394+
395+
This syntax has been dropped and is no longer supported with the new `scala` runner.
396+
```bash ignore
397+
scalac hello.scala
398+
scala hello # NOTE: this syntax is not supported by Scala CLI
399+
# Hello
400+
```
401+
402+
With Scala CLI, all inputs have to be passed explicitly, so any compiled classes in the current working directory
403+
would be ignored unless passed explicitly.
404+
```bash ignore
405+
scalac hello.scala
406+
scala-cli run -cp .
407+
# Hello
408+
```
409+
410+
:::note
411+
If only the classpath is passed with `-cp`, then the `run` sub-command can't be skipped, as otherwise Scala CLI
412+
will default to the REPL (as there are no explicit source file inputs present).
413+
414+
```bash ignore
415+
scalac hello.scala
416+
scala-cli -cp .
417+
# Welcome to Scala 3.5.0 (17, Java OpenJDK 64-Bit Server VM).
418+
# Type in expressions for evaluation. Or try :help.
419+
#
420+
# scala>
421+
```
422+
:::
423+
424+
It is possible to explicitly specify the main class to be run (for example, if there are multiple main classes
425+
in the build). The `run` sub-command becomes optional then, as passing `-M` indicates the intention to run something.
426+
```bash
427+
scalac hello.scala
428+
scala-cli -cp . -M hello
429+
# Hello
430+
```
431+
432+
:::note
433+
If you want to compile your sources with a separate command, and then run them later, you can also do it
434+
with the `compile` sub-command, rather than the `scalac` script.
435+
436+
You don't have to specify the class files location, Scala CLI won't recompile them if they are up to date.
437+
```bash ignore
438+
scala-cli compile hello.scala
439+
scala-cli hello.scala
440+
# Hello
441+
```
442+
443+
Alternatively, you can also specify the location for the compiled classes explicitly, and then add them
444+
to the classpath, as you would with `scalac`.
445+
```bash ignore
446+
scala-cli compile hello.scala -d compiled_classes
447+
scala-cli hello.scala
448+
# Hello
449+
```
450+
:::
451+

0 commit comments

Comments
 (0)