sweetyBaby sweetyBaby - 6 days ago 5
Scala Question

How to run the Scala code in Eclipse

I am new to Scala, I write the simple code in scala, and tried to run it, but when I clicked "run as", it can not run, May I know the problem?

class Object {
def main(args: Array[String]){
val myVar: Int=0
val myString: String="a"
var myVar1 = 20
val myVal1 = "Hello Scala new without datatype declaration."
println(myVar1)
}
}


enter image description here

Answer

1.Make sure you have Scala plugin installed in your Eclipse or possibly you can download and use Scala IDE.

2.If your project is Maven,then right click your project, configure->Add Scala Nature.

3.Your code shows you are having your main method in Scala class file but main methods are meant to be used in Scala Object file. In Scala we have separate file type called object files.

object MyObject {
  def main(args: Array[String]){
    val myVar: Int=0
    val myString: String="a"
    var myVar1 = 20
    val myVal1 = "Hello Scala new without datatype declaration."
    println(myVar1)
  }
}
Comments