Running Scala Code as a Stand-alone Script

Most operating systems support the shebang syntax to run arbitrary scripts. We can use that approach to run stand-alone files with Scala code in it. This eliminates the needs to explicitly invoke the scala command and works seamlessly as long as Scala is installed on the system.

Running as a Stand-alone Script on Unix-like Systems

On Unix-like systems, set the shebang preamble in the script like this:

FirstStep/hello.sh
 
#!/usr/bin/env scala
 
println(​"Hello "​ + args(0))

Make sure the file hello.sh has executable permission by typing chmod +x hello.sh. Then to run it, type the following command on the command line:

 
./hello.sh Buddy

Buddy is the argument that is passed to the script. Here’s the output from the call:

 
Hello Buddy

Running as a Stand-alone Script on Windows

You can configure Windows to invoke Scala directly on a double-click of a stand-alone .scala file. To do that, within Windows Explorer, double-click a Scala script file with the .scala extension. Windows will complain that it can’t open the file and will ask you to select a program from a list of installed programs. Browse to the location where Scala is installed, and select scala.bat. Now you can run the program by simply double-clicking it in Windows Explorer, or you can run it from the command prompt without adding the scala extension to the filename.

When you double-click the program within Windows Explorer, you will notice that a window pops up, displays the result of execution, and quickly shuts down. To keep that window open, you can point the file to a bat file that will run Scala and pause. To do this, right-click the Scala program, select “Open With...,” and browse to and select the bat file.

Here’s an example:

FirstStep/RunScala.bat
 
echo off
 
cls
 
call scala %1
 
pause

If you double-click HelloWorld.scala, our setup will now automatically run the RunScala.bat file, and you should see the following output appear:

images/GettingStarted/runninginvista.png

So far we’ve looked at running Scala from the command line, but you can also run it from an IDE.

IDE Support for Scala

Java developers make heavy use of IDEs to develop applications. All major IDEs—Eclipse, IntelliJ IDEA, NetBeans—have plug-ins to assist with Scala development. They provide facilities similar to what we’re used to when programming in Java—syntax highlighting, code completion, debugging, proper indentation, and so on. Furthermore, we can mix and reference Scala and Java code in the same project.

Depending on your favorite IDE, simply install the appropriate plug-in. If you’re using lightweight editors such as Sublime Text and TextMate, look for builders or bundles for Scala.

We have run Scala code as script so far and avoided explicit compilation. As a program grows in size to more than one file or into multiple classes, you’ll want to compile it. Let’s take a look at the steps.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.21.248.119