Tuesday, June 16, 2015

How to execute a scala HelloWorld program in java?

-- First create a HelloWorld.scala file with the below contents by using any text editor.

object HelloWorld {
    def main(args: Array[String]) {
        println("Hello, world!")
    }
}

-- compile it to get a scala class file

> scalac HelloWorld.scala

-- this will produce HelloWorld.class file

-- to execute using scala do the below

> scala HelloWorld

-- to execute in java, you need to provide the class path to one external scala jar, which is available in scala/lib folder, name: scala-library.jar

> java -cp .:/path/to/scala/lib/scala-library.jar HelloWorld

Note: provide full path to the jar file instead of relevant path or shortcuts like ~, ../.. etc.