Saturday Aug 17, 2013

10 Steps to Happiness with Scala and NetBeans IDE

Took me about 20 minutes to figure out the procedure below, including downloading the related software. At the end, you'll have the samples from the Scala distribution up and running in NetBeans IDE. Click to enlarge the image below:

Let's get started.

  1. Get NetBeans IDE. Download NetBeans IDE 7.3.1, which is the latest official release of NetBeans IDE at the time of writing. If all you want to do is use Scala in NetBeans IDE, make sure to download the "Java SE" distribution, since that's the smallest distribution providing the basic toolset on top of which you will install the Scala plugin.

  2. Get Scala. Download Scala and set it up so that when you run Scala on the command line, it works. That's a nice way to check that you have downloaded and installed Scala correctly.

  3. Connect Scala to NetBeans IDE. Open the "etc/netbeans.conf" file, in the installation directory of NetBeans IDE, and use "-J-Dscala.home" in "netbeans_default_options" to point to your Scala installation. For example, on Ubuntu, my "netbeans_default_options" in "etc/netbeans.conf" is now as follows:
    netbeans_default_options="--laf Nimbus -J-Dscala.home=/home/geertjan/scala/scala-2.10.2 
    -J-client -J-Xss2m -J-Xms32m -J-XX:PermSize=32m -J-Dapple.laf.useScreenMenuBar=true -J-Dsun.java2d.noddraw=true 
  4. Install the NetBeans Scala Plugin. Start NetBeans IDE and go to Tools | Plugins and install all the Scala modules which, together, constitute the NetBeans Scala plugin:

  5. Verify Scala is Connected to NetBeans IDE. After installing all the modules shown above, go to Tools | Scala Platforms. Each of the tabs, "Classes", "Sources", and "Javadoc" should show some content, based on a combination of the Java platform and the Scala platform.

  6. Take a Look at the Project Templates. Go to the New Project wizard (Ctrl-Shift-N) and notice there are three new project templates in a new category appropriately named Scala:

  7. Import the Scala Samples into NetBeans IDE. Select "Scala Project with Existing Sources" and click Next. Name the new project "scala-samples" and place it anywhere you like on disk, e.g., in the NetBeans Projects folder:

    Click Next. Browse to the root folder of your Scala download, i.e., in my case, "/home/geertjan/scala/scala-2.10.2".

    Then click Next again. In the "Excludes" field, exclude all the folders that are not named "examples". That is, we're creating a new NetBeans project based on the "examples" folder in the Scala distribution, hence we don't want all the non-examples folders in our project.

    Click Finish and now you have a NetBeans Scala project consisting of all the samples in the Scala distribution:

  8. Fix Import Statements. Right-click and build the project and notice that there are some Scala packages that are incorrectly declared. I.e., the Duration class is in a different package than that which is declared in some of the examples, so use the error annotations and hints to correct those occurrences.

  9. Define Run Configurations. I noticed that individual files can't be run, i.e., no right-click and Run File. So go to the toolbar and create some run configurations:

    In each case, simply type the full path to the class you want to run:

  10. Run the Project. Finally, when you run the project, and you have a main class defined as shown in the previous step, you'll see the result in the Output window:

Seems to me that the NetBeans Scala plugin is very mature. Syntax coloring, code completion, etc. Nice.


Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.


« August 2013 »