9

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.

Chris Martin
  • 30,334
  • 10
  • 78
  • 137
Steven Edmunds
  • 315
  • 6
  • 14

2 Answers2

20

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
}
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thanks, that did it. The webpage I got that bad code from was http://www.scala-lang.org/documentation/getting-started.html. Was that code only correct for an earlier version of Scala? It's kind of discouraging to have so much trouble getting a simple example that was just meant to test the installation. – Steven Edmunds Jul 05 '14 at 04:57
  • 3
    @StevenEdmunds I don't see your initial code in that page. Either you don't extend `App`, and have a `main` function, or you do extend `App`, and don't have a `main` function. Both cases are illustrated in the page you refer. – VonC Jul 05 '14 at 04:59
  • Yeah, you're right again, I was pasting the contents of the object into the scala application template created by the Eclipse IDE, and didn't notice the difference. I think I get too impatient and frustrated when doing things like setting up and configuring environments, and I start getting sloppy and overlooking things. I appreciate you taking the time to straighten me out on all this. – Steven Edmunds Jul 06 '14 at 20:14
0

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")

    }
}
Rahul Biswas
  • 185
  • 1
  • 8