About Abhijeet Sutar

Abhijeet (ajduke) is self-taught, self-organized software developer. His current go to language is Java and also he is exploring other languages such as Scala, Ruby.

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.scala-big-

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

png;base64e0eb56e75df37d2f

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.

png;base64eeb0818d339067c3

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:

png;base645511ddab82462dcf

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

png;base64af03475578e2f7e0

png;base644b9f329609859c0
 

Reference: Various ways to run Scala code from our JCG partner Abhijeet Sutar at the Another Java Duke blog.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

Leave a Reply


eight − = 0



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close