简体   繁体   中英

How do I compile Scala Hello World application

I'm new to Scala, and I've never written or compiled a program in it before. I'm trying to simply run the following Hello World example, which I have saved in a file name scalaApp.scala

object scalaApp extends App {
    def main(args: Array[String]) {
      println("Hello, world!")
    }
}

When I go the terminal in the file's directory and type "scalac scalaApp.scala", I get the following error message:

scalaApp.scala:4: error: overriding method main in trait App of type (args:    Array[String])Unit;
 method main needs `override' modifier
    def main(args: Array[String]) {
        ^
one error found

I thought I'd followed all the directions to install Scala 2.10.3 correctly on my computer, but I don't know how to test it since I can't even compile this simple program. If I type "scala" into the terminal I do get a scala prompt on which I can run commands like "1 + 1". I'm not sure how much that shows. I have added the SCALA_HOME variable to ~/.profile, and added SCALA_HOME to the PATH variable in ~/.profile. If anyone could tell me what I'm doing wrong, or give me a suggestion on where I might find an answer, I'd appreciate it.

Since App extends DelayedInit , you shouldn't define a main function

This should be enough:

object scalaApp extends App {
     println("Hello, world!")
}

The compiler creates this function for you, and will pass it into the delayedInit(x: => Unit) method (notice the call-by-name in the parameter ).

The compiler will emit:

object Main extends DelayedInit {
  def delayedInit(x: => Unit = { println("Hello, worl!") }) = // impl is left for us to fill in
}

When you are compiling a .jar file, I encountered the error.

spark-submit-2.2 sequential-assembly-1.0.0-SNAPHOST.jar
Error: No main class set in JAR; please specify one with --class

Then I appended my code to

object getdata{

    def main(args: Array[String])={
        println("Hello World")

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM