Getting Started with IntelliJ IDEA Scala Plugin

Skip to end of metadata
Go to start of metadata

This getting started guide is intended for those who want to start working with the Scala IntelliJ IDEA plugin. Hope it gives you a good kick to start exploring the plugin for yourself, and, of course, reading this space - you can find more useful information about other helpful plugin features, like Java/Scala cross-language interoperability which includes refactorings, coding assistance and much more.
In this guide:

Before You Begin

Currently, IntelliJ IDEA Scala plugin is in beta stage and is being actively developed. Here is the list of what you will need to try it:

  • One of the latest IntelliJ IDEA EAP builds (can be downloaded from IntelliJ IDEA EAP page).
  • Scala SDK should be downloaded and installed on your computer.

Setting up the Environment

After you have downloaded, installed and configured all the prerequisites, follow this simple procedure to get the Scala plugin up and running.

  1. Run IntelliJ IDEA. On the Welcome Screen, click Open Plugin Manager.
  2. Press Browse repositories button and type Scala in the Search box to quickly locate plugin in the list.
  3. Right-click plugin name, select Download and Install from the menu, and restart IntelliJ IDEA to apply the changes.
  4. After IntelliJ IDEA is back, open Plugin Manager again to examine the list of installed plugins. If everything is OK, there should be Scala plugin, listed in black, with a tick mark next to its name. All this means the plugin is installed, enabled and is working OK. If there's no tick mark, or plugin name is listed in red, or you can't find it at all - there may be some problem you need to address, like getting the latest IntelliJ IDEA EAP version.
    There is an alternative way to install the Scala plugin.Download the Scala plugin from the Nightly Builds for Leda page and unzip the archive to the directory <user home>/.IntelliJIdea<xx>/config/plugins. After that, run IntelliJ IDEA.

Creating a Project

If everything is fine, you're ready to create the Scala project with IntelliJ IDEA.

  1. Back to the Welcome Screen, click Create New Project. The good old IntellIJ IDEA New Project wizard apperas. Here you choose to create a new project from scratch:
  2. All you need is a Java Module with a name that makes sense, located somewhere you can find it later without spending a whole day digging through your entire hard drive, and the src directory that will store your source code:
      
  3. On top of that, select Scala on the Select Desired Technologies. By default all the necessary jars will be downloaded automatically, but you also may pick them from an already existing IDEA library or a secret place on your hard drive.
  4. It's possible to adjust the level of a library you want to create. Select Global or Project from the Level drop-down list.
  • If you plan to create other projects that involve this library, select Global - thus you will be able to share the library anywhere on the current machine.
  • If you select Project level, then all the jar archives will be included in your project. This makes your project portable between the different computers and independent from the Scala SDK.
To significantly speed-up project compilation you may configure the Fast Scala Compiler (FSC).

Exploring Project Structure

Now, when the project is ready, it is advisable to explore its settings. To do that, on the main toolbar, click . In the Project Structure dialog, click Modules, and then select the Dependencies tab:

Next, pay attention to the Scala facet added to our module:

As you see, IntelliJ IDEA has automatically specified the Scala compiler library, and added it to the list of module dependencies. If you want to learn more about project configuration, refer to http://devnet.jetbrains.net/thread/290032.

Creating Source Code

Now everything is ready for the first HelloWorld application.

  1. Right-click src folder (or press Alt-Ins in default Windows IntelliJ IDEA keymap) and select Package. Type the package name helloworld. Then, right-click the newly created package and choose Scala Class on the context menu:
  2. In the dialog box that opens, select the type of the Scala entity to be created - in our case, let it be Scala Object, type HelloWorld as the object name, and click OK. Here is our first Scala object.
    You can skip first step. Instead of HelloWorld you can type helloworld.HelloWorld then appropriate package will be created automatically.
    To choose 'Object' in the combobox you can use up and down arrows on your keyboard, just to do it slightly faster.
  3. Now, we're going to make it runnable. There're several ways to achieve that. First off, we can extend the default scala.App trait. Just type App and space, you even don't need to use code completion, which will work automatically (see the picture below).

    Or, we can use a more Java-like approach: create main() method inside of object body. Again, IntelliJ IDEA lends us a hand with Live Templates. All we do is type main and press Tab (or press Ctrl+J in the default Windows IntelliJ IDEA keymap, and select main from the suggestion list). IntelliJ IDEA expands this macro to a complete well-formed main() method body.
  4. Let's teach the application to do some work. If you're familiar with IntelliJ IDEA, you should probably be aware of that running a simple Java application is just a matter of pressing Ctrl+Shift+F10 (on default Windows keymap). Good news is that it works equally well for Scala applications.
    All we have to do is to type the code that displays the trivial Hello World message and then press Ctrl+Shift+F10 - or right-click the editor background and choose Run HelloWorld.main on the context menu. The application output is displayed in the console.
     
    You can also turn your Scala object into a script. Just remove all declarations from the file, leave the executable statement only - println("Hello, World!"), and run the script as described above.
Labels:
None
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.
  1. Jun 20, 2009

    Anonymous

    This does not work (IntelliJ 8.2.1 OS-X). There is no Ctrl+Shift+F10nor Cmd+Shift+F10 available at all. menu -> Run is disabled.

    1. Jun 25, 2009

      Anonymous

      You must add a target in Run>Configure. This is apparently ignored in the tutorial.

    2. Aug 20, 2009

      Anonymous

      Doesn't work on 8.1.3 correctly. I don't get code completion.

  2. Jul 19, 2009

    Anonymous

    Small annoyance, but the format coder always inserts a line between every declaration:

    def a: Int
    def b: Int
    def c: Int

    becomes

    def a: Int

    def b: Int

    def c: Int

    1. Jul 21, 2009

      Hello.

      Yes, this is a good point. Line spacing should be available to adjust.

  3. Jul 21, 2009

    Anonymous

    Hi,

    I'm trying out IDEA mainly due to the bad state of the scala 2.8 plug-in for eclipse. I read somewhere that IDEA can also already be used to develop for 2.8 (what is the currently instable trunk). If I follow the above instructions, I come to select from a Scala version that goes just up to 2.7.5. How can I develop a scala 2.8 plug-in in IDEA?

    Thanks, -sciss-

    1. Jul 21, 2009

      Hi.

      Using instructions from here you can build Scala plugin for IntelliJ which is able to work with Scala 2.8.

      1. Jul 21, 2009

        Anonymous

        hi ilya,

        thanks for the fast response. i had to do export ANT_OPTS="-Xmx512m" before i could run ant (maybe that's worth adding to your text). Then, unfortunately, the scalac task ends with 43 errors.... http://pastebin.com/m363c0f8e&nbsp; is there something out of sync?

        1. Jul 21, 2009

          Anonymous

          oh wait... the page points to a version called "Maia", and i got "Diana", i guess there's the problem... hold on, i will retry...

          1. Jul 21, 2009

            Yep, that's the right point.

        2. Jul 21, 2009

          Hi.

          Judging by the log you've attached, you have wrong version of IDEA SDK. Are you sure that idea.home property in your case points to the actual installation directory of the last IntelliJ EAP (10558)?

  4. Jul 31, 2009

    Anonymous

    Followed all the instructions but trying to compile the plugin for Maia 10597 and 2.8 is giving 2830 errors.

    1. Jul 31, 2009

      Yes, thins is known problem caused by major changes in Scala compiler and related to packaging system. Take a scala compiler version from before 24th of July 2009.

  5. Aug 31, 2009

    Anonymous

    Is it possible to run on object on mac os or not?
    I keep getting just a console window. If I want that I can go back to emacs.
    I keep hearing how much better this is than Eclipse but honestly not feeling it.
    Can some detail how to get the same result as ctrl-shift-F10 on mac please!?

    1. Aug 31, 2009

      If you have no problems with compilation, this might be a bug in a type system of the plugin. If your object doesn't inherit the `Application' trait, but uses an explicit main method, put type `Unit' to it signature explicitly. Hope, it helps.

  6. Aug 31, 2009

    Anonymous

    No compilation errors.
    I am just using a main.
    type Unit gave same result.

    Please assume I know nothing, because I don't.

    I have a valid HW.scala. what do I do in ide ver 8.1.3?
    code completion works, etc.
    I'm on shiny new mac running Darwin.
    How do I run my object?

    Someone mentioned you have to configure something?

    I see an option to run a scala script but nothinh to run an app/object?

    1. Aug 31, 2009

      These symptoms look very similar, isn't this your case?

      http://www.jetbrains.net/devnet/message/5244366#5244366

      1. Aug 31, 2009

        Anonymous

        Yep that was it.
        Hadn't realized I installed that other Scala plugin as well.
        Thanks Ilya.

  7. Sep 04, 2009

    Anonymous

    I have MAIA 10652. I downloaded the Scala plugin from the plugin page (http://plugins.intellij.net/plugin/?id=1347) and installed it but I didn't have code completion or any Scala recognition by the IDE that I expected.

    It was a simple fix, I detailed the steps I took on my blog: http://www.digitalchickenscratch.com/2009/09/idea-intellij-9-maia-and-scala/

  8. Feb 21, 2010

    Anonymous

    I had tried to run helloworld in "scala script" run configuration, that was successfully executed with no output. And in compiler output directory there are no files.
    object HelloWorld
    def main(args: Array[String])

    Unknown macro: { println("HelloWorld") }

    Please write instructions on run configuration.

    1. May 13, 2010

      Anonymous

      No responses in 3 month and it doesn't work so far. This plugin is shit.

      1. May 13, 2010

        Anonymous

        Yeah, not so much activity from here lately.
        Hope they are on vacations or something like that and will be back soon
        As for your problem, I have never seen such, maybe you forgot to check "Make" in run configuration details?
        Also the common pitfall is absence of scala compiler in project dependencies or mismatched version of compiler in facet configuration.

  9. Apr 19, 2010

    Anonymous

    I'm using IntelliJ 9 and all this instructions don't work. I keep getting "class not acceptable".

  10. Apr 22, 2010

    Anonymous

    Tried latest IntelliJ CE 9.0.2 and any scala version and I can never get the compiler working.

    Always gets this:
    Scalac internal error: class java.lang.ClassNotFoundException [java.net.URLClassLoader$1.run(URLClassLoader.java:200), java.security.AccessController.doPrivileged(Native Method), java.net.URLClassLoader.findClass(URLClassLoader.java:188), java.lang.ClassLoader.loadClass(ClassLoader.java:315), sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:330), java.lang.ClassLoader.loadClass(ClassLoader.java:250), java.lang.ClassLoader.loadClassInternal(ClassLoader.java:398), java.lang.Class.forName0(Native Method), java.lang.Class.forName(Class.java:169), org.jetbrains.plugins.scala.compiler.rt.ScalacRunner.main(ScalacRunner.java:72)]

    Back to Emacs for me after many many hours...

    1. Apr 29, 2010

      Anonymous

      I have the same problem. Bummer, looks like a cool plugin but if HelloWorld doesn't work on a Mac and it's tough to diagnose, then it will likely be a tough call to use this plugin on a much larger project.

    2. May 07, 2010

      Anonymous

      I had a similar problem.

      The problem I found was that it is compiling Java 1.6 by default.

      You need to tell it the source or target is 1.5.

      Not sure which one is important or where I found the option. Works now though.

      Have fun.

      1. Jun 20, 2010

        Anonymous

        Thanks, switching to 1.5 did the trick for me.

  11. May 03, 2010

    Anonymous

    Got the same problem.

    Fix:
    Open "Module Settings", select the project and go to the "Dependencies" tab.
    Check the box before "scala-2.8.0.Beta1" (or the scala version you selected when you created the project)

  12. May 03, 2010

    Anonymous

    There was more to the story :/
    I did a little fiddling to get it working and thought only the last step was enough to get it working, so:

    1: Download the latest Scala version (2.8.0.RC) and unpack it.
    2: Open "Module Settings" - select "Scala" facet
    3: Replace "Scala compiler library" and "Scala SDK library" with the downloaded versions.
    4: Compile (it will probably fail, but with a different error)
    5: Go back to the "Scala" facet and change back to the original library versions
    6: Check "scala-2.8.0.Beta1"
    7: Compile
    And now it will work.

    1. May 21, 2010

      Anonymous

      Thanks a lot !

    2. Jun 25, 2010

      Anonymous

      i think it's even easier than that.

      1. create a new scala project, and allow it to download the correct libs

      2. open "module settings" -- select scala facet

      3.  tick the box "use additional jars for compiler or SDK"

      works for me.  strange it doesn't work out of the box though.

  13. Jul 09, 2010

    Anonymous

    Hi,

    I've just installed the scala plugin with IJ 9.02.
    I can add classes/objects but whenever I want to compile a scala class nothing seems to happen.
    When I run the app via the main-method I get the following error:

    "Exception in thread "main" java.lang.ClassNotFoundException: ScalaTest"

    In the "Project Structure" view I have "Scala" enabled under "Facets" and
    the scala-compiler.jar & scala-library.jar enabled under "Libraries".

    Any hints?
    Permalink
    Reply

  14. Jul 26, 2010

    Anonymous

  15. Feb 24, 2011

    Is there a forum to discuss the Scala Plugin for Idea?

  16. Feb 15, 2013

    Seriously please update this , for IDEA 12.x , there's now a Scala module and its not quite clear where to get the sdk, the dropdown doesn't automatically fine what I'm guessing would be the jdk, so this needs updated instructions