I want to build .scala file.
Preparation
This post assumes configurations in this earlier post.
Method
With sbt
Installing sbt
sbt stands for Simple Build Tool and is Scala’s de facto build tool.
- Install sbt from here.
- Create system variable
SBT_HOMEand set it to the path of the sbt installation. - Add
%SBT_HOME%\binto path. - Open
cmdand typesbt. Wait for everything to install and the interactive mode withsbt:scala>to pop up.
Building project
- Create a
.scalafile containing, say, the following code.object Hi{ def main(args: Array[String]) = println("Hello world!") } - Open
cmd,cdto the directory containing the.scalafile, and typesbt. - Type
run. The output should look something like this:C:\Users\steve-rogers\scala>sbt [warn] Neither build.sbt nor a 'project' directory in the current directory: C:\Users\steve-rogers\scala c) continue q) quit ? c Java HotSpot(TM) Client VM warning: ignoring option MaxPermSize=256m; support was removed in 8.0 [warn] No sbt.version set in project/build.properties, base directory: C:\Users\steve-rogers\scala [info] Set current project to scala (in build file:/C:/Users/steve-rogers/scala/) [info] sbt server started at local:sbt-server-62d2810ebd461b523e48 sbt:scala> run [info] Updating ... [info] Done updating. [info] Compiling 1 Scala source to C:\Users\steve-rogers\scala\target\scala-2.12\classes ... [info] Done compiling. [info] Packaging C:\Users\steve-rogers\scala\target\scala-2.12\scala_2.12-0.1.0-SNAPSHOT.jar ... [info] Done packaging. [info] Running Hi Hello world! [success] Total time: 5 s, completed Jul 31, 2019 11:28:17 PM sbt:scala>
Without sbt
- Create a
.scalafile containing, say, the following code.object Hi{ def main(args: Array[String]) = println("Hello world!") } - Open
cmd,cdto the directory containing the.scalafile, and typescalac hello.scala. - Observe that the files
Hi$.classandHi.classare created. The latter is what we need to execute. Typescala Hi. The output should look something like this:C:\Users\steve-rogers\scala>scala Hi Hello world!