Why I can't execute scala file?

前端 未结 9 2046
日久生厌
日久生厌 2020-12-10 02:49

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:

pr         


        
相关标签:
9条回答
  • 2020-12-10 03:32

    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!"
    
    0 讨论(0)
  • 2020-12-10 03:33

    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>
    
    0 讨论(0)
  • 2020-12-10 03:38

    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

    0 讨论(0)
提交回复
热议问题