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 Answer
Reset to default 1You 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
- https://github/Baeldung/scala-tutorials
- https://github/jetbrains-academy/scala-tutorial
- https://github/deanwampler/spark-scala-tutorial
本文标签: Running Scala from a script using terminalStack Overflow
版权声明:本文标题:Running Scala from a script using terminal - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745589839a2665131.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
=
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:
with=
? – Abhijit Sarkar Commented Nov 18, 2024 at 23:04