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.
Related Whitepaper:

Java Essential Training

Author David Gassner explores Java SE (Standard Edition), the language used to build mobile apps for Android devices, enterprise server applications, and more!

The course demonstrates how to install both Java and the Eclipse IDE and dives into the particulars of programming. The course also explains the fundamentals of Java, from creating simple variables, assigning values, and declaring methods to working with strings, arrays, and subclasses; reading and writing to text files; and implementing object oriented programming concepts. Exercise files are included with the course.

Get it Now!  

Leave a Reply


× 2 = two



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use
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.

Sign up for our Newsletter

15,153 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books
Get tutored by the Geeks! JCG Academy is a fact... Join Now
Hello. Add your message here.