run object scala in databricks

Viewed 1146

I am trying to execute sample code on databricks in scala. It is an object.

object Main {
  def main(args: Array[String]) {
    val res = for (a <- args) yield a.toUpperCase
    println("Arguments: " + res.toString)
  }
}

When I run on databricks; it says 'object defined main'. I am not sure how to execute it now or what is the code to execute it. Please help.

2 Answers

What you are working with is kind of scala REPL. Basically "main" function does not have any significance over there. Having said that you can run your function as follows

object Main {
  def main(args: Array[String]) {
    val res = for (a <- args) yield a.toUpperCase
    println(res)
    println("Arguments: " + res.toString)
  }
}

Main.main(Array("123","23123"))

As is you can call Object Main's main method.

You can call the main method in the Main object as follows:

val args: Array[String] = Array("test1", "test2", "test3")
Main.main(args)

What you have in your main method won't print what you expect, which I assume is the values contained in the res array. To accomplish that you would need to change it to something like the following:

object Main {
  def main(args: Array[String]): Unit = {
    val res = for (a <- args) yield a.toUpperCase
    println("Arguments: " + res.mkString(" "))
  }
}
Related