How do I compile Scala Hello World application
Asked Answered
A

2

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.

Abercromby answered 5/7, 2014 at 4:30 Comment(0)
G
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
}
Gad answered 5/7, 2014 at 4:43 Comment(3)
Thanks, that did it. The webpage I got that bad code from was 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.Abercromby
@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.Gad
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.Abercromby
B
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")

    }
}
Bugleweed answered 31/8, 2017 at 16:47 Comment(1)
bash-4.2$ spark-submit-2.2 sequential-assembly-1.0.0-SNAPHOST.jar Hello World bash-4.2$Bugleweed

© 2022 - 2024 — McMap. All rights reserved.