Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run scala code on Intellij Idea 11?

I am new to intellij idea and I decided to shift because I found the scala plugin on eclipse to be annoyingly buggy. But, intellij idea, which I have heard to be very good, looks difficult to use. I looked through the getting started with scala plugin page but couldn't find documentation specific to the latest version of lightweight ide of intellij idea 11 for scala.

I have created a project and made a hello world object in scala but I am not able to run it. On trying to run it, it shows the scala interpreter (type in expressions to get them evaluated). I don't exactly understand what changes to make in the project structure to be able to run scala code. As of now, i added the jsdk to point to my $JAVA_HOME and the scala library is also added. The scala plugin is also working in that syntax highlighting etc is fine.

The intellij idea user interface is also not very friendly and I don't get how to run the code. What modules am I supposed to add? Also, does compiled scala code run with jvm? I'll be grateful to anyone who helps out.. I have been putting off writing scala code because I am not able to find THE IDE.

EDIT: Thanks for all the help! I am now able to run a basic scala program. I am trying to add external jars. What is the equivalent of doing Project right click-> Configure Build Path -> adding external jar files in eclipse? I tried adding "dependencies " under modules tab in project structure. It's adding the jar files but somehow the classes in the jar files are not still recognised.

like image 305
rivendell Avatar asked Mar 05 '12 07:03

rivendell


People also ask

Why Scala class is not showing in IntelliJ?

This can be resolved by following the following simple steps. Just right click your project and select Add Framework Support from the context menu shown. If you have Scala plugin installed, you should see the option for Scala available.

How do I run a Scala code in terminal?

Executing Scala code as a script Another way to execute Scala code is to type it into a text file and save it with a name ending with “. scala”. We can then execute that code by typing “scala filename”. For instance, we can create a file named hello.


2 Answers

With the Scala plugin installed either

  • A. Create a new project and select the Scala facet in the creation phase. It will create the Scala library lib and Scala compiler lib and setup the facet for you

  • B. If you already have a project. Go to Project Structure -> Modules and right click the module and go Add facet and add a Scala facet. Now you need to add scala-library.jar as a library of the module and go into the Scala facet and point it to a library containing scala-compiler.jar

Some more information

enter image description here

this is what your module should look like under project settings

enter image description here

select the Scala facet and this is what you should see (Library name for the compiler is unimportant as long as it says (version xxx) next to the library name

enter image description here

these are the jar files in my scala-compiler lib

enter image description here

and these are the jar files in my scala-library lib

With everything setup like that you should be able to right click -> new Scala class (Select object from the dropdown) in a source directory and add

def main(args:Array[String]) {
   println("Hello world")
}

then right click on the class and select Run Classnam.main() and IntelliJ will setup a run configuration for you.

like image 117
UberMouse Avatar answered Oct 24 '22 21:10

UberMouse


Edit: I can see someone posted somewhat the same I want to say here. I hope this can give a few more in-depths hints, so I will post it anyway :-)

If you would like to run Scala native in IntelliJ (I shifted to IntelliJ and still can't get my hands down) you need to make sure a few things are in order. Most of this happens under File -> Project Structure which requires you to create a project (when you get a hang of it, it is much more logical than Eclipse -- and Netbeans for that matter). I am also assuming you have selected a jdk, but this can also be done in the Project Structure.

Now, to Scala:

  1. Make sure you have the Scala plugin. Visit the Settings (File -> Settings; can also be found at the start-up screen) and locate 'Plugins' (for me it's nr. 8 from the bottom). Under "Browse Repositories" you should be able to search for "scala", find and install a plugin. 1 A. If no plugins pops up you are probably using a version of intellij where scala is not (yet) supported. If that happens you can go to the homepage for the plugin and download the nightly build.
  2. Import Scala as a library in File -> Project Structure -> Global Libraries. Click the plus sign, select Java (Scala is a Java-lib), browse to your scala-dir (which can be downloaded here: http://plugins.intellij.net/plugin/?id=1347) and select the lib directory. Press ok and you should see the library popping up on the list.
  3. Make sure the facet (Scala framework) is in place. Goto File -> Project Structure -> Facets. Click on the plus-sign and find Scala. A sublink should appear (if not double-click the Scala link). Clicking the sublink make sure you have selected a compiler before continuing. This should be provided by the modules selected before. I normally use FSC (Fast Scala Compiler) but it's probably a good idea to test the basic settings before experimenting.
  4. Make sure the Scala Module is in place. In Project Structure -> Modules click on the module you want to compile in scala and click the plus icon above and select Scala. Again be sure to select a compiler library.
  5. If you want to, setup configurations for the compiler under the facet
like image 44
Jens Egholm Avatar answered Oct 24 '22 21:10

Jens Egholm