Sunday, March 19, 2017

First Scala Application in Eclipse -Windows

Here we shall learn to write simple Scala application so that you can try the legacy Hello World application yourself. Before moving forward, its required to set the environment by following my post.

Creating new Scala application

1. Go to "File" - "New" - "Other..." and select "Scala Project" from the folder "Scala Wizards"
2. Choose a project name and select "Finish"


3. Create package by right clicking on "src" folder. Give a name and click finish.



4. Select "File" - "New" - "Scala Object" to create a new object



5. Write statements/logic of application. This scala object is similar to Java's Main method class. For this, we can follow any of the two approach, 1) either extend "App" to this object or 2) explicitly write main method.

extend App 

use "main" method



You would  have noticed that each statement doesn't end with semicolon(";") . Yeah , Scala saved us from the ceremony to type semicolon as the compiler is made smart to determine the termination of statements. You can still type semicolon if needed but its void by Scala compiler.

6. Save the application and Run by right clicking on the Scala object and choose "Run as" - "Scala Application"


7. Output would be printed at the console


we successfully wrote our first Scala application !!!

Alternatively,If you want existing project to be imported in Eclipse, can be done as similar to importing Java or other applications, "File" - "Import" - "Existing Projects into Workspace". If still finding problem importing the project, then the project is compiled but not compatible to use in Eclipse. To make so, we need to compile project to support Eclipse. Open terminal and either go to the root directory where SBT is installed in order to make sure any applications in the system is compatible to use in Eclipse or navigate to the project folder path to make specific project compatible. Type the command

C:\Users\Profile> sbt eclipse
You should see response something like this

[info] Successfully created Eclipse project files for project(s):
[info] YourProjectNameHere
If there is an error then there would be problem with the plugins.sbt file setup under SBT root directory.To fix this, navigate to .sbt folder and check for plugins folder.  If not found then create a folder naming "plugins" and inside the plugins create a file named plugins.sbt. write the following into the file


addsbtplugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "2.5.0")
Reload the SBT from terminal by passing the command

> reload
Well done !!! So now import the project and you should see the project in Eclipse.

Since you are familiar with running Scala application, lets lecture on how to use REPL environment in Scala. If not familiar with REPL please read from here. Having said, we need to create Scala worksheet by going to "New"- "Scala WorkSheet"


Give a name to your worksheet and click "finish".

Now you can write each statement in the worksheet and see the results towards right.


So easy !!!!. Have fun with Functional Programming !!!


No comments:

Post a Comment