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 !!!


Saturday, March 18, 2017

Using Scala in Eclipse - Windows


Scala is Programming Language that supports Functional Programming. Scala source code is intended to be compiled to bytecode, resulting in flexibility to be run on Java Virtual Machine.In order to work on Scala, you need to have the following tools installed on your machine:

  • Java Development Kit (JDK)
  • Scala Build Tool (SBT)
  • Editor such as Eclipse,Intellij IDE or any other IDE of your choice

Check whether you have Java installed by typing in the console:

java

or check the latest version

java -version

If not installed then download java from the Oracle website ,run installer and include bin directory of installed JDK in Path environment variable as explained here.

Download SBT tool from here. Run the installer and include the bin directory (usually located under C:\Program Files\sbt\bin) in Path environment variable. Check if installed by typing in console

sbt

you will see something like 

Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=256m; support was removed in 8.0
[info] Set current project to Users (in build file:/C:/Users)

Also you can find the folder .sbt installed under C:\Users\User_Profile\.sbt

Lastly we focus on choosing IDE. Mostly recommended is Intellij IDE as it showcase the environment to run Scala application, but here I will spotlight on using Eclipse.

For Scala we have two options to use Eclipse, 1) either use any existing eclipse IDE (Kepler onwards is preferred as Scala plugin is not supported below Kepler) or 2) use Scale IDE for Eclipse which is very easy - download and use in 2 minutes.

If using regular Eclipse, choose Help/Install => New Software and paste the URL http://download.scala-ide.org/sdk/lithium/e46/scala211/stable/site. You would be provided with the options to install Scala for Eclipse IDE, accept it and download completes. Eclipse will restart.

Now you can create new Scala project if you find the Scala related options under File =>New

Good job guys 😊, Hope this blog helps you !!!