Develop Hello World Program using Scala and run the application

Develop Hello World Program using Scala


In this topic, we will about Scala program

  • To create a new project, click on a New project and select Scala and choose SBT.
  • Name the project, select the JDK.
  • Select the SBT version of 0.13.17 and Scala version 2.11.12, click Finish.
  • Select the build.sbt, and click on Import Changes.
  • In src –> main –>Scala, right click create new Scala Class.
  • Create a sample program “Hello World”.
                object HelloWorld {
                     def main(args :Array[String]):Unit={
                     println("Hello" + args(0))
                     }
                     }
  • If you want to add existing main class click on Run/Debug configurations, select Applications.
  • Enter the class name, choose the main class and pass program arguments.

Setup SBT and run application HelloWorld


In the above video,  we will setup sbt and run the Helloworld application.

  • SBT is Simple Build Tool.
  • To download SBT 0.13 version, click here
  • Install SBT in your system.
  • Once the installation is done, launch Command Prompt.
  • Copy the path from the project-based directory and paste it in command prompt.
  • Run the SBT package, then it creates the .jar files.
  • Once jar files are created, type sbt run.
  • Then type sbt “run-main program-name arguments”.
  • It displays the result.
  • If your project is having only one object with the main function, you can directly use sbt run to validate the .jar file runs.
  • If your project is having multiple objects with the main function or the main function takes arguments then you can use sbt run main.

Add spark dependencies to the application


In the above video, we will see how to setup spark dependencies to the application.

  • Go to build.sbt, we need to define library dependencies.

libraryDependencies += “org.apache.spark” %% “spark-core” % “2.3.0”

  • In this org.apache.spark is group id, spark-core is an artifact and 2.3.0 is version.
  • Save the file and make sure that the repositories are downloaded.
  • Enable the auto-import option.
  • We can use Spark based API’s as a part of Scala applications.