可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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:
println("HelloWorld!")
when I typed scala
, there was no problem, the scala REPL was shown, but when I typed scala Hello.scala
, it prompted:
<console>:1: error: ';' expected but '.' is found.
I dont know what problem is, I hope someone can help me.
回答1:
Adding -nc
flag saved my day.
scala -nc Hello.scala
What is -nc
?
-nc no compilation daemon: do not use the fsc offline compiler
Source: http://alvinalexander.com/java/jwarehouse/scala-2.11/compiler/scala/tools/nsc/GenericRunnerCommand.scala.shtml
What is fsc offline compiler
?
fsc – Fast offline compiler for the Scala 2 language
Source: http://www.scala-lang.org/old/sites/default/files/linuxsoft_archives/docu/files/tools/fsc.html
回答2:
To execute external script (load all definitions from it) in REPL use :load <filepath>
command instead of 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>
回答3:
Your file Hello.scala
is a script. You should be able to run it from the command prompt with 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:
scala> println("HelloWorld!")
"HelloWorld!"
回答4:
If you are executing it in scala REPL .You should be using as below
:load [scala file path]
output would be as below
defined "objectname"
Then invoke the main method as the next command
objectname.main(Array())
Refer the below url for more detailed info
https://www.scala-lang.org/documentation/getting-started.html
回答5:
Just a summary of what I know. I used to be confused by how to run a scala file properly.
In sum, we got 3 ways to achieve this:
For script files. For script, it means it's just as the same as you type lines of statements in the REPL, or use :paste
mode to paste multiple lines. Something like:
println("foo")
Compile an object containing main method with scalac
. You can extends
App trait to easily implement.
def main(args: Array[String]) : Unit
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.
scala -howtorun:object ObjectName -cp "./"
: The -cp
is just for insurance.
java -cp "/path/to/your/scala-jars" ObjectName
: So the difference is just the classpath need to be included.
回答6:
If you’re on some flavor of Unix, you can run a Scala script as a shell script
by prepending a pound bang directive at the top of the file.
For example, type the following into a file named helloarg
:
#!/bin/sh
exec scala "$0" "$@"
!#
// Say hello to the first argument
println("Hello, "+ args(0) +"!")
The initial #!/bin/sh
must be the very first line in the file.
Once you set its execute permission:
$ chmod +x helloarg
You can run the Scala script as a shell script by simply saying:
$ ./helloarg globe
If you’re on Windows, you can achieve the same effect by naming the
file helloarg.bat
and placing this at the top of your script:
::#!
@echo off
call scala % 0 % *
goto :eof
::!#
回答7:
Write the println
statement inside the main function:
object HelloWorld {
def main(args: Array[String]) {
println("Hello World!")
}
}
Then execute: scala -nc HelloWorld.scala
回答8:
This works in mac for development.
#!/usr/bin/env scala -nc
println("hello");
回答9:
When you are trying to run scala follow below steps:
- Dont open scala REPL
- use
scala <filename.scala><Input if any>
from cmd prompt
below is the example:
scala /home/prakash/Desktop/babyname.scala < /home/prakash/Desktop/ND
Note: above command must be typed in unix cmd prompt not in scala repl.