5

I am just beginning with my scala development on the Scala IDE(Eclipse). I am trying to create a new project and write a sample hello world program to kick things off. This is my sample program:

object hello {
  def main(args: String) = {
    println("Hello World!");
  }
 }

I am using Java 8. I don't see any errors in the program. However, when I try to run the program, I get an error like this:

Error: Main method not found in class hello, please define the main method as:
   public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application

I am now clueless. Why is it asking me to create a main function with Java syntax? And why is it throwing an error when there are no problems with the code(As far as I know)? I tried searching for answers on the existing questions but none of them are about scala development.

Any help would be appreciated. Thank you.

Hemanth Annavarapu
  • 823
  • 3
  • 19
  • 37

5 Answers5

8

Your main must take an array of string. It currently takes a single string

From scala's official website :

object HelloWorld {
  def main(args: Array[String]): Unit = {
    println("Hello, world!")
  }
}

https://www.scala-lang.org/documentation/getting-started.html

Also, make sure you are using the "run as Scala application" option in Eclipse.

litelite
  • 2,857
  • 4
  • 23
  • 33
  • 1
    Thanks for the answer. Tried it but still getting the same error. – Hemanth Annavarapu May 25 '17 at 20:10
  • 2
    Are you using "run as Scala application"? – litelite May 25 '17 at 20:14
  • Yes, I tried running as a scala application and it is working now. Thanks a lot! :) – Hemanth Annavarapu May 25 '17 at 20:16
  • I am having the same issue however, it didn't work with any of these options. Eclipse IDE still shows red cross mark on project level but not on any file. I don't know what is wrong. I have tried to def main method like def main(args:Array[String]): Unit = { println("Helo") } and def main(args:Array[String]) { println("Hello") } – Piyush Patel Mar 02 '18 at 18:23
2

You have a class with tha same name in tests.

Check that your test folder does not contain object hello. Check yuor test package for unique object names. After renaming, problem must be solved.

There is no difference how to implement main method. You can do like this:

object hello {
  def main(args: String) = {
    println("Hello World!");
 }
}

Or like this:

object hello extends App {
    println("Hello World!");
 }

```

Dmitry Kaltovich
  • 2,046
  • 19
  • 21
  • You're 1st example won't run. It suffers from the same problem as the original question and can be fixed by following the advice from the accepted answer. – jwvh Oct 09 '18 at 07:05
  • This actually fixed my problem. – Rabzu Mar 05 '20 at 22:21
1

This may not help each and everyone, But I did face the same issue, I cleaned the project and it worked!!!

Omkar Kulkarni
  • 139
  • 1
  • 1
  • 10
1

In my case (Intellij Ultimate 2018.1), I had a test with the same name than the object with the main method:

  • src/main/scala/com/xx/xx/MyApp
  • src/test/scala/com/xx/xx/MyApp

when tried to dun main...MyApp failed with the exception in this question.

Just renamed src/test/scala/com/xx/xx/MyApp to MyAppTest

surfealokesea
  • 4,971
  • 4
  • 28
  • 38
0

In my case I had both a companion object and a class with the same name, which is normally okay in Scala but appears to have confused Java.

Renaming either the object or class to a different name solved the problem.

GreenGiant
  • 4,930
  • 1
  • 46
  • 76