scala 为什么我不能执行scala文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12399385/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 04:31:40  来源:igfitidea点击:

Why I can't execute scala file?

scala

提问by user1653363

I am a newbie to Scala, it's the first time I'm running Scala, when I installed Scala, I created a file named Hello.scala, the content is:

我是 Scala 的新手,这是我第一次运行 Scala,当我安装 Scala 时,我创建了一个名为 的文件Hello.scala,内容是:

println("HelloWorld!")

when I typed scala, there was no problem, the scala REPL was shown, but when I typed scala Hello.scala, it prompted:

当我输入时scala,没有问题,显示了scala REPL,但是当我输入时scala Hello.scala,它提示:

<console>:1: error: ';' expected but '.' is found.

I dont know what problem is, I hope someone can help me.

我不知道是什么问题,我希望有人可以帮助我。

回答by Mingyu

回答by om-nom-nom

To execute external script (load all definitions from it) in REPL use :load <filepath>command instead of scala <filepath>.

要在 REPL 中执行外部脚本(从中加载所有定义),请使用:load <filepath>命令而不是scala <filepath>.

? echo 'println("HelloWorld")' > Hello.scala
? scala
Welcome to Scala version 2.9.2 (OpenJDK Client VM, Java 1.6.0_24).
Type in expressions to have them evaluated.
Type :help for more information.

// is this what you tried to do?
scala> scala Hello.scala
<console>:1: error: ';' expected but '.' found.
   scala Hello.scala

// do this instead
scala> :load Hello.scala
Loading Hello.scala...
HelloWorld

scala>

回答by dhg

Your file Hello.scalais a script. You should be able to run it from the command prompt with scala Hello.scala.

您的文件Hello.scala是一个脚本。您应该能够从命令提示符运行它scala Hello.scala

$ scala Hello.scala 
HelloWorld!

The REPL, on the other hand, is not for running scripts. It is used for running scala code directly:

另一方面,REPL 不用于运行脚本。它用于直接运行 Scala 代码:

scala> println("HelloWorld!")
"HelloWorld!"

回答by kiran Guntur

If you are executing it in scala REPL .You should be using as below

如果你在 scala REPL 中执行它。你应该使用如下

:load [scala file path]

output would be as below

输出如下

defined "objectname"

定义的“对象名”

Then invoke the main method as the next command

然后调用main方法作为下一个命令

objectname.main(Array())

Refer the below url for more detailed info

有关更多详细信息,请参阅以下网址

https://www.scala-lang.org/documentation/getting-started.html

https://www.scala-lang.org/documentation/getting-started.html

回答by vishwas vadavi

Write the printlnstatement inside the main function:

println在main函数里面写语句:

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

Then execute: scala -nc HelloWorld.scala

然后执行: scala -nc HelloWorld.scala

回答by wliao

Just a summary of what I know. I used to be confused by how to run a scala file properly.

只是我所知道的总结。我曾经对如何正确运行 Scala 文件感到困惑。

In sum, we got 3 ways to achieve this:

总之,我们有 3 种方法来实现这一点:

  1. For script files. For script, it means it's just as the same as you type lines of statements in the REPL, or use :pastemode to paste multiple lines. Something like:

    println("foo")

  2. Compile an object containing main method with scalac. You can extendsApp trait to easily implement.

    def main(args: Array[String]) : Unit

  1. 对于脚本文件。对于脚本来说,就和你在 REPL 中键入语句行一样,或者使用:pastemode 粘贴多行。就像是:

    out.println("foo")

  2. 编译一个包含 main 方法的对象scalac。您可以通过extendsApp trait 轻松实现。

    def main(args: Array[String]) : 单位

You may say "You liar, there are just two ways". But for the second one, I think there's too ways to get it running.

你可能会说“你这个骗子,只有两种方法”。但是对于第二个,我认为有太多方法可以让它运行。

  1. scala -howtorun:object ObjectName -cp "./": The -cpis just for insurance.
  2. java -cp "/path/to/your/scala-jars" ObjectName: So the difference is just the classpath need to be included.
  1. scala -howtorun:object ObjectName -cp "./":-cp只是为了保险。
  2. java -cp "/path/to/your/scala-jars" ObjectName: 所以区别只是需要包含类路径。

回答by nazar_art

If you're on some flavor of Unix, you can run a Scala script as a shell script by prepending a pound bangdirective at the top of the file.

如果您使用的是某种 Unix,您可以通过在文件顶部添加一个pound bang指令来将 Scala 脚本作为 shell 脚本运行。

For example, type the following into a file named helloarg:

例如,在名为 的文件中键入以下内容helloarg

#!/bin/sh
exec scala "
$ chmod +x helloarg
" "$@" !# // Say hello to the first argument println("Hello, "+ args(0) +"!")

The initial #!/bin/shmust be the very first line in the file.
Once you set its execute permission:

首字母#!/bin/sh必须是文件中的第一行。
一旦你设置了它的执行权限:

$ ./helloarg globe

You can run the Scala script as a shell script by simply saying:

您可以将 Scala 脚本作为 shell 脚本运行,只需说:

::#!
@echo off
call scala % 0 % *
goto :eof
::!#

If you're on Windows, you can achieve the same effect by naming the file helloarg.batand placing this at the top of your script:

如果您使用的是 Windows,您可以通过命名文件helloarg.bat并将其放在脚本顶部来实现相同的效果:

#!/usr/bin/env scala -nc
println("hello");

回答by Deepak

When you are trying to run scala follow below steps:

当您尝试运行 scala 时,请按照以下步骤操作:

  1. Dont open scala REPL
  2. use scala <filename.scala><Input if any>from cmd prompt
  1. 不要打开scala REPL
  2. 使用scala <filename.scala><Input if any>从命令提示符

below is the example:

下面是示例:

scala /home/prakash/Desktop/babyname.scala < /home/prakash/Desktop/ND

scala /home/prakash/Desktop/babyname.scala < /home/prakash/Desktop/ND

Note:above command must be typed in unix cmd prompt not in scala repl.

注意:以上命令必须在 unix cmd 提示符下输入,而不是在 scala repl 中。

回答by jichi

This works in mac for development.

这适用于 mac 进行开发。

##代码##