Various ways to run Scala code

For running example in this tutorial, make sure that, you have latest Java distribution and Scala distribution installed on your machine and environment variable SCALA_HOME points to base directory of the scala installation and %SCALA_HOME%/bin added to PATH variable.

using Scala REPL

It is basically command line interactive shell called as REPL short for Read-Eval-Print-Loop.

To start scala REPL, open command prompt and simply type scala.  After that, you will see new scala prompt waiting for your input as shown below

Now, you can type of any scala expressions or code in prompt and hit enter and you will get the output immediately as shown below.

using Scala interpreter to run scala script

You can save your scala code in the file with .scala extension  (basically with any file extension but prefered .scala extension) and to run, provide file name with extension as parameter to scala interpreter

Create the file HelloScala.scala with following code

val str = "Hello "+ "Scala "
println("'str' contents : "+str)

Now, we can run this file using command shown as follows

shell> scala HelloScala.scala
'str' contents : Hello Scala

As you can observe we do not require the any class definition declaration or so. We can put the code inside the file and we are ready to run it.

using Scala interpreter

Usual scala program contains lot code chunks spread of across lot of files, for running these programs we need to go through two stages, compile the scala source code using scala compiler and run the compiled bytecode using scala interpreter.

Lets create file named Hello.scala with following code

object Hello {
     def main(args:Array[String]):Unit = {
       println("Hello, Scala !! ")
}
}

Little explanation about above program, we created object Hello, Object is way scala represent the static members and inside it we have main method taking param as array of strings and returning Unit which is same as void in Java. This main method more like one in java but scala version of it.

compile file using scala compiler scalac, as shown below,

shell > scalac Hello.scala

It will create the couple of class files in current directory. To run this, we use scala interpreter (or java interpreter, a little later on this ) by passing the class name (not with .scala or .class extension). In our case, we do following

shell > scala Hello
Hello, Scala !!

using Java interpreter

As compiled Scala code is bytecode that we can run with Java interpreter which is java.exe or java.sh shipped with standard Java JRE distribution. But for this we need to put additional library in classpath.

We just need to add the scala-library.jar which located under $SCALA_HOME/lib

To run using java interpreter, we use following command

shell > java -cp $SCALA_HOME/lib/scala-library.jar;.  Hello

using Scala worksheet

This scala worksheet is part of Scala IDE for eclipse. It is like the REPL but much more convenient and powerful than REPL.

Following is excerpt from official github repo wiki about the scala worksheet

A worksheet is a Scala file that is evaluated on save, and the result of each expression is shown in a column to the right of your program. Worksheets are like a REPL session on steroids, and enjoy 1st class editor support: completion, hyperlinking, interactive errors-as-you-type, auto-format, etc.

For creating new scala worksheet in scala ide, first create scala project then right click on scala project and go to following New > Scala WorkSheet

It will prompt for name for worksheet and folder to which worksheet to be created. Give any name, accept default folder and then hit enter

After that you will get the worksheet  as shown as follows. it gives output at right of your code (one marked in red) as shown following figure

You can write any code inside object body and hit the save button and you have the output at right of your code

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s