admin管理员组

文章数量:1432178

I am really sorry, this seems basic. I am using Programming in Scala and I have got to step 4:

Put this into a file named hello.scala:

@main def m():
        println("Hello world from a script!"

At first I got an illegal start of definition error, since pivoting to using vs code I get the following:

(base) *dir* scala % scala hello.scala
Compiling project (Scala 3.5.1, JVM (21))
[error] ./hello.scala:2:12
[error] end of toplevel definition expected but '(' found
[error]     println("hello world from script")
[error]            ^
[error] ./hello.scala:1:11
[error] Declaration of method m not allowed here: only classes can have declared but undefined members
[error] @main def m():
[error]           ^
Error compiling project (Scala 3.5.1, JVM (21))

I thought it might be a new line issue, so I moved everything onto one line like @main def m(): println("hello world from script") and I get the following:

(base) *dir* scala % scala hello.scala
Compiling project (Scala 3.5.1, JVM (21))
[error] ./hello.scala:1:23
[error] end of toplevel definition expected but '(' found
[error] @main def m(): println("hello world from script")
[error]                       ^
[error] ./hello.scala:1:11
[error] Declaration of method m not allowed here: only classes can have declared but undefined members
[error] @main def m(): println("hello world from script")
[error]           ^
Error compiling project (Scala 3.5.1, JVM (21))

Using scalac hello.scala does not work either.

I am really sorry, this seems basic. I am using Programming in Scala and I have got to step 4:

Put this into a file named hello.scala:

@main def m():
        println("Hello world from a script!"

At first I got an illegal start of definition error, since pivoting to using vs code I get the following:

(base) *dir* scala % scala hello.scala
Compiling project (Scala 3.5.1, JVM (21))
[error] ./hello.scala:2:12
[error] end of toplevel definition expected but '(' found
[error]     println("hello world from script")
[error]            ^
[error] ./hello.scala:1:11
[error] Declaration of method m not allowed here: only classes can have declared but undefined members
[error] @main def m():
[error]           ^
Error compiling project (Scala 3.5.1, JVM (21))

I thought it might be a new line issue, so I moved everything onto one line like @main def m(): println("hello world from script") and I get the following:

(base) *dir* scala % scala hello.scala
Compiling project (Scala 3.5.1, JVM (21))
[error] ./hello.scala:1:23
[error] end of toplevel definition expected but '(' found
[error] @main def m(): println("hello world from script")
[error]                       ^
[error] ./hello.scala:1:11
[error] Declaration of method m not allowed here: only classes can have declared but undefined members
[error] @main def m(): println("hello world from script")
[error]           ^
Error compiling project (Scala 3.5.1, JVM (21))

Using scalac hello.scala does not work either.

Share Improve this question asked Nov 18, 2024 at 22:55 cluelessclueless 111 silver badge1 bronze badge 3
  • 1 You are missing an = methods don't start with :, rather : is used to specify the return type. – Luis Miguel Mejía Suárez Commented Nov 18, 2024 at 23:03
  • What happens if you replace the : with =? – Abhijit Sarkar Commented Nov 18, 2024 at 23:04
  • Thank you @LuisMiguelMejíaSuárez – clueless Commented Nov 19, 2024 at 14:14
Add a comment  | 

1 Answer 1

Reset to default 1

You need to keep something like this in the file:

object HelloWorld {
  def main(args: Array[String]): Unit = {
    println("Hello, World!")
  }
}

Then save that file say hello.scala

from same folder in terminal

scalac HelloWorld.scala

then

scala HelloWorld

Another way is to do a script like

@main def m(): Unit =
  println("Hello, world from a script!")

This you can run directly by calling

scala hello.scala

Its great to read a book but might help to also watch a few videos that have screen share on so can see the actions and maybe get some code ~ search like this

  1. https://github/Baeldung/scala-tutorials
  2. https://github/jetbrains-academy/scala-tutorial
  3. https://github/deanwampler/spark-scala-tutorial

本文标签: Running Scala from a script using terminalStack Overflow