2

I have just started learning scala, i am using the eclipse ide for it, in the run configuration i have set scala application with the project name and as main class main when i compile i have

Error: Could not find or load main class main

when i check the console i see it's reading from Java/jre directory, is it normal or should i change that ? This is the code

package one

class Main {
object Bottles {
    def main(args: Array[String]){
        var n : Int=2;
        while(n<=6){
            println(s"Hello ${n} bottles");
            n+=1;
        }
    }
}
}
T4l0n
  • 595
  • 1
  • 8
  • 25

2 Answers2

2

Okay, also had the same error Error: "Could not find or load main class main" in my Scala IDE and the reason of that was that when I created Main, I immediately moved it to a package.

So I had to:

  • Move my main class back to default package.
  • Clean, compile, run.
Vladimir Stazhilov
  • 1,956
  • 4
  • 31
  • 63
  • package should not have been a problem. It is recommended to always use a package name. Probably you only needed to do a clean after fixing the code. – puhlen Nov 15 '16 at 16:25
  • No I did clean and it didn't help, why do you say clean is a silver bullet? – Vladimir Stazhilov Nov 16 '16 at 10:32
  • @puhlen: this might be due to the "Java Build Path" being set to `src` , not `src/main/scala`. Any idea how to fix? – serv-inc Jan 29 '19 at 11:48
1

The main method needs to be on a toplevel object. Your Bottles object is wrapped in a Main class. Remove that Main class and your code should run.

object Bottles {
  def main(args: Array[String]){
    var n : Int=2;
    while(n<=6){
      println(s"Hello ${n} bottles");
      n+=1;
    }
  }
}
Reactormonk
  • 21,472
  • 14
  • 74
  • 123