NEWBIE: running my hello world Scala program

Hi,

For previous discussion regarding my problem see this thread: http://devnet.jetbrains.net/message/5288513#5288513

I have a trivial hello world program I'd like to run:

package hello

object HelloWorld {
   def main(args: Array[String]): Unit = println("hello")
}


I can compile it IDEA and I can run it in the command line with:

scala hello.HelloWorld

My problem is that I'm unable to run this in IDEA (10.0.2 on OSX). In the abovementioned thread Taras Tielkes guided me to run it like this:

Right mouse button menu -> "Run Hello.main()" entry.
Pressing Ctrl-Shift-F10 while you are in the body of the main() method should work as well.

My problem is that I don't see the "Run Hello.main()" entry in the right-click menu. I tried over the code window and in the project tree over the file, but it's not there.

Ctrl-Shift-F10 does nothing, as does the "Run > Run..." menu entry. It is not greyed out, but it does nothing, no error message or anything.

What am I doing wrong?

Also, I created the file with "New > Scala class", but the icon is not a Scala strairway, but a generic yellow circle with an O in it. If I change the "object" to "class" the icon changes to a Scala class icon, so how come IDEA (or the plugin?) does not recognize an object as a Scala entity?

8 comments
Comment actions Permalink

Hi Alex,

Welcome to the forum :)

In general it would be useful to know which version of IntelliJ, and which version of the Scala plugin you are using.
The first one you can check using menu Help->About
The second one you can check by going to Settings (Ctrl-Alt-S) -> Plugins -> Scala

I see in the original thread that you've recently updated to IDEA 10.0.2.
Please make sure that you're using the latest official release of the Scala plugin (as older versions seem to have some incompatibilities with 10.0.2).
Current "stable" version is #0.4.573 (http://plugins.intellij.net/plugin/?idea&id=1347)

Your example works for me as expected.
I've attached a screenshot that shows IntelliJ offering the "Run..." option:
Untitled.png
Perhaps something is not set up properly in your project configuration.

First thing to check: is the directoy containing your sources is properly marked as a source root?
Go to "Project Structure" (Ctrl-Shift-Alt-S) -> "Sources" tab to check this.

Second thing to check: how did you set up Scala itself (scala-compiler jar and scala-library jar). Are these properly configured as a library?

Third and last thing to check: does your module have a Scala facet configured?

If your project is just a simple "Hello, world", you could attach it perhaps.

Kind regards,
-tt

0
Comment actions Permalink

Thanks for the reply!

It seems that my setup is borked somehow. This is weird, because I've not really done anything special with my  setup, just installed 10.0.1 it some time ago, and tried to get some Scala  stuff working. Problems started when I upgraded to 10.0.2.

My Scala plugin version is 0.4.407, but when I try to update it, it says I need also the com.intellij.spring plugin, and when it's trying to install that I get "com.intellij.spring: error in opening zip file". How do I resolve this? Can I install the Scala plugin manually with dependencies?

The my src dir is a source folder, and the project has a Scala facet. I set up Scala by pointing the facet at scala/ in my home directory, and it figured out the libraries automatically. I can see the libraries in the Global Libraries tab in the Project Structure view.

I can replicate the situation like this:
- Create new project from scratch (default)
- Create java module (default)
- Create source directory (src, default)
- Check the Scala facet
- Use scala distribution (point it to scala/, it finds the libraries)
--> Finish to create the project
- Create a Scala class named Hello in /src
- Paste the contents in my above message

Right-clicking the file shows no run option. Screenshot:
Screen shot 2011-02-23 at 16.19.20.png

I can also send the project if needed.

0
Comment actions Permalink

The problem with updating to the latest version is a known one, and should be fixed in the upcoming IDEA 10.0.3 release.

In the mean time, there are two workarounds you can try:
1) Invoke "Update" from the "Available" tab in the plugin manager screen (instead of the "Installed" tab)
2) Download the zip file (linked from http://plugins.intellij.net/plugin/?idea&id=1347) manually, exit IntelliJ, and extract it to ~.IntelliJIdea10/config/plugins (make sure to delete the old Scala directory there first)

-tt

0
Comment actions Permalink

Updating the plugin from the "Available" tab did the trick, now I can run my object :) Thanks!

This is not the same issue, but I noticed that when I create a new Scala class using the new plugin it generates some comments, and this:

package ;
public scala class Test { }


Probably a regression in the plugin.

Anyway thanks for your help, now I'm able to continue exploring Scala in a proper IDE.

0
Comment actions Permalink

Hi Alex,

This second problem is a known one too.
Fortunately, there's a workaround as well :)

See the following discussion thread in this forum:
http://devnet.jetbrains.net/message/5286355#5286355

In short, do the following:
1) Exit IntelliJ
2) From ~.IntelliJIdea10/config/fileTemplates, remove everything named like Scala
3) Restart IntelliJ, and invoke "File"->"Invalidate Caches" (this will trigger another restart of IntelliJ)

After that, the outdated templates should be gone forever.

Kind regards,
-tt

P.S. A very nice option to enable for the Scala plugin is the so-called "Type aware highlighting":
Go to "Settings" (Ctrl-Alt-S) -> "Code Style" -> "Scala" -> "Other settings" -> Enable "Type-aware highlighting" checkbox.
This will cause IntelliJ to report many code errors (similar to what IntelliJ and other IDEs do for Java).

0
Comment actions Permalink

Hi,

The fix worked like a charm. For reference, the folder with the invalid template is ~/Library/Preferences/IntelliJIdea10CE/fileTemplates/internal/ on OSX.

The type aware highlighting feature is very nice too. I hope it will be enabled by default when it's finished :)

Thanks for your help!

0
Comment actions Permalink

Hi Alex,

I think "when it's finished" depends on getting feedback on things that don't work well.

You can submit bug reports (and feature requests) through the bug tracker:
http://youtrack.jetbrains.net/issues/SCL

-tt

0
Comment actions Permalink

Since 0.4.589 version (released) Scala plugin suggests to perform required actions automatically:

screenshot1.png

0

Please sign in to leave a comment.