Scala Play Hello World

Posted on Wednesday, August 21, 2013


I am going to create a scala play hello world application on a Windows 7 machine and on an Ubuntu server.

This guide goes over setting up the tools and environment, and does not get beyond the default Hello World much.



Installing Scala on Win7


In this section I will go over installing Scala in a Windows 7 environment.  This guide assumes you already have Java 1.6 or later installed on your system.

First we need to download  Scala for windows

Go to http://www.scala-lang.org/downloads and download scala. (a the time of this writing it was version 2.10.2)




Click on the Download link.  This will download the MSI installer provided by scala.  Open it up and run the installer.






Click on Next.




Accept the license and click Next.






Make sure the location is OK for you and click Next.






Click Install.







Wait while Scala installs.




Click Finish



Testing win7


Now open a command prompt and run this command


> scala -version





In this case it shows that its version 2.10.2


Cygwin


I use Cygwin a lot, so I want to make sure Scala is working on it as well.

If I open a Cygwin window and run the following command.


> scala -version





It looks like it's working, but I have this warning

cygwin warning:
  MS-DOS style path detected: C:\Program Files\Java\jdk1.6.0_25/bin/java
  Preferred POSIX equivalent is: /cygdrive/c/Program Files/Java/jdk1.6.0_25/bin/java
  CYGWIN environment variable option "nodosfilewarning" turns off this warning.
  Consult the user's guide for more details about POSIX paths:
    http://cygwin.com/cygwin-ug-net/using.html#using-pathnames


To fix this I did the following… 

In my case I have defined JAVA_HOME in my System Properties


Cygwin is using this path

If I run the following command


> echo $JAVA_HOME






It returns the MS-DOS style path.

You can use the cygpath command to convert paths from MS-DOS style to unix style.

If you run this command you will see how it works.


> cygpath $JAVA_HOME





You can see how it converts the path.


One way to permanently fix this is to edit the .bash_profile.

Open the .bash_profile


> vi .bash_profile


And add the following to the bottom.


export JAVA_HOME=$( cygpath "JAVA_HOME" )


Opening a new Cygwin window and running scala -version results in








Install the Play Framework on Windows 7


Go to the following web site to download scala play http://www.playframework.com/download



Click the download button.
It's a big file 147MiB, give it a minute to download


The official Installation Instructions from the playframework folks can be found at http://www.playframework.com/documentation/2.1.2/Installing [1]

Here are my simple notes, which are based in part on playframework's install notes.





I created a Play folder on my C: drive and unzipped the contents of the downloaded file into it.


C:\Play\play-2.1.3 needs to be added to the path variable





Click on Start,  right click on Computers and click on Properties





Click on Advanced System Settings




Click On Environment Variables




Select Path and click Edit



Enter the  ;C:\Play\play-2.1.3 at the end of the variable value and click OK (do not forget the  ;  before the path)




Now a quick test.   Open up a DOS prompt and run this command


  > play help




You should get this notice, that displays a few bits of information about the play framework.




Cygwin


Now to get it working on cygwin.

If I run the following command in cygwin


  > play help




I get the following error.

/cygdrive/c/Play/play-2.1.3/play: line 64: JAVA_HOME/bin/java: No such file or directory

I tried to get it working and got a little frustrated until I found this post https://groups.google.com/forum/#!topic/play-framework/HR6WtivxWWY [2] where Michael Slinn posted a simple fix that worked for me.



Open the .bash_profile


> vi .bash_profile


And add the following to the bottom.


export PLAY_HOME=/cygdrive/c/Play/play-2.1.3
alias play='$PLAY_HOME/play.bat'


Running play help from cygwin



Works!


Install Scala Play on Ubuntu


Installing Scala on Ubuntu 12.10


From the command line you could run the following,  though I personally do it differently as I want to use oracle's version of Java vs OpenJDK


> sudo apt-get install scala


If you do this on Ubuntu 12.10 you get Scala 2.9.2 using OpenJDK




That is all well and good but I would rather use Java 1.7 from Sun and the latest scala build 2.10.0 so to do that install you need to first install java 1.7 to do that run the following commands


>  sudo apt-get purge openjdk*
>  sudo apt-get install software-properties-common
>  sudo add-apt-repository ppa:webupd8team/java
>  sudo apt-get update
>  sudo apt-get install oracle-java7-installer
>  java -version


.



Run the following command to download the .tgz file from scala and untar it


  > wget http://www.scala-lang.org/files/archive/scala-2.10.2.tgz
  >  tar xf scala-2.10.2.tgz


Now place the scala program in /usr/bin/scala
And make a ln to it



    > sudo mkdir /usr/lib/scala
    > sudo mv scala-2.10.2 /usr/lib/scala/
    > sudo touch /usr/bin/scala
    > sudo ln -fs /usr/lib/scala/scala-2.10.2/bin/scala /usr/bin/scala
    >  sudo chmod a+x /usr/bin/scala






Testing Ubuntu


Open a new terminal and run scala


  > scala



Here you can see scala is using Java 1.7_0_25.



Install the Play Framework on Ubuntu


Download Scala Play and unzip it


> sudo apt-get install unzip
> wget http://downloads.typesafe.com/play/2.1.3/play-2.1.3.zip
> unzip play-2.1.3.zip

  
Create a location to put the play software, put it in /usr/lib/play
And make a ln to it


    > sudo mkdir /usr/lib/play
    > sudo mv play-2.1.3 /usr/lib/play
    > sudo touch /usr/bin/play
    > sudo ln -fs /usr/lib/play/play-2.1.3/play /usr/bin/play
    >  sudo chmod a+x /usr/bin/play



Now a quick test.   Run this command


  > play help






And it works!



Create your first Scala Play Application


Run the following command to create a Scala Play Application (either from ubuntu or win7)


    > play new HelloWorld




Click enter then when presented with the option of scala or java use scala by entering 1


This creates a HelloWorld folder in the current directory.



Test It

Now that the web application has been created run it.


> cd HelloWorld
> play ~run





The default is to listen on port 9000

Now open a web browser and open this location.  If its running locally open http://127.0.0.1:9000/




The HelloWorld WebApp is Running.




Update some code


The play framework, out of the box, will auto compile when any files are updated.

Test this out real quick

Leave the program running, then run the following in another command prompt to edit a program file.  Make sure you are in the HelloWorld Web App Folder.

Open up HelloWorld/app/controllers/Application.scala


> vi app/controllers/Application.scala





Edit "Your new Appliction is ready" to "Hello World"



Save the file.  (play will automatically recompile it)

Then reload the Web App




The page has been updated.


Running on Port 80

Another quick interesting tidbit for those who would rather run it on port 80.  Use this command to start your server (after stopping it of course by pressing ctrl+d)


> play -Dhttp.port=80 ~run



If you are running from Ubuntu use the following command instead


> sudo play -Dhttp.port=80 ~run


(Remember port 80 is a priveleged port J )


Now open the web site using port 80

http://127.0.0.1/          





From my Win 7 machine I got an error…

Turns out Skype listens on port 80 and already had control of the port.  If I quit Skype, it works.







Fixing Skype

Now that I have noticed the problem with Skype listening on port 80, I want to fix it. 




From the Tools menu select Options.





Click on Advanced
Then click on Connections




Uncheck the "Use port 80 and 433 as alternatives"

Quit Skype and reopen it.   
  
This fixed my issue.  This may be something you can't do as Skype attempts to use port 80 and 443 to get by firewalls, if your company tries to block programs like Skype.


References
[1]  Installing Play
       Visited 8/2013
[2]  Play 2.0 - Horrible hack for running under Cygwin

       Visited 8/2013 

No comments:

Post a Comment