如何从命令行运行Scala文件? [英] How to run scala file from command line?
问题描述
scala是否支持 scala run xxx.scala
?go语言支持像
Does scala support scala run xxx.scala
? The go language supports running like
go my.go
和python支持
python my.py
但似乎
scala xxx.scala
仅是语法检查,没有观察到输出或运行行为.那么有没有办法直接运行scala文件?
is only a syntax check, no output or running behavior is observed. So is there a way to run a scala file directly?
推荐答案
这似乎很容易.但是您必须使用println来打印某些内容.
It seems pretty easy. But you have to use println to print something.
➜ ~ cat hello.scala
println("hello, world")
➜ ~ scala hello.scala
hello, world
➜ ~ scala -Vprint:parser hello.scala
[[syntax trees at end of parser]] // hello.scala
package <empty> {
object Main extends scala.AnyRef {
def <init>() = {
super.<init>();
()
};
def main(args: Array[String]): scala.Unit = {
final class $anon extends scala.AnyRef {
def <init>() = {
super.<init>();
()
};
println("hello, world")
};
new $anon()
}
}
}
hello, world
➜ ~
或者有几种方法可以将行输入到REPL,为您打印结果.
or there are a few ways to feed lines to the REPL, which prints results for you.
➜ ~ cat calc.scala
2 + 2
➜ ~ scala < calc.scala
Welcome to Scala 2.13.1 (OpenJDK 64-Bit Server VM, Java 11.0.3).
Type in expressions for evaluation. Or try :help.
scala> 2 + 2
res0: Int = 4
scala> :quit
➜ ~
另请参见 -i
, -I
, -e
和:load
和:paste
命令.
See also -i
, -I
, -e
, and the :load
and :paste
commands.
如另一个答案所示,它还将在对象中寻找Java风格的main方法.通常,这就是编译程序入口点的方式.
As shown in the other answer, it will also look for a Java-style main method in an object. That's how you'd normally compile a program entry point.
这篇关于如何从命令行运行Scala文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!