Stuck at "Hello World" with IntelliJ IDEA 9.0.1 for Scala
Asked Answered
O

4

7

I've been using Eclipse since 2.x and IDEs in general for over 20 years (since Turbo Pascal and Turbo C in the late '80s!).

(that preamble is supposed to imply, "I'm not an idiot" ... but doesn't sound so smart as I read it... LOL :-] )

Now I'm trying to use the Scala debugger in IntelliJ 9.0.1. I've resigned myself to an old standby, the "hello world" trick to check if the environment is setup correctly:

class hello {
  def main(a: Array[String]) = println("got args: " + a)
}

I also tried this version, just in case:

object hello extends Application {
    println("hi")
}

Alas, I'm unable to get even this simple Scala example to run. I'd like to eventually put a breakpoint in it, but for now just running it would be great. I have Java 1.6u20 and the Scala plug-in 0.3.473 (January 2010). The error below summarizes my experience:

alt text

What possibly could I be doing wrong?

Thanks

Octofoil answered 19/4, 2010 at 5:12 Comment(3)
Alright, got it... removing the '=' from def main on the first example fixes it! I guess the type signature of main() still matters :) However, the second example should work but doesn't. Any ideas ??? (my actual problem is solved, but now I'm curious!) ThanksOctofoil
I recommend using 9.0.2 EA with the latest Scala plug-in. JetBrains' EA releases (especially for point releases) are almost always of near-release quality. And while the Scala plug-in people come up woefully short in the release-notes department, the plug-in has improved steadily over the past several months and keeping up with the latest is worthwhile.Linkous
On a side note: there are issues with Application trait and it's deprecated now. Use App instead.Heck
D
6

From your screenshot it looks like you were using:

class hello {
  def main(a: Array[String]) = println("got args: " + a)
}

The main method has to be on an object to support a main method.

Capitalizing the object / class name is the convention but it isn't enforced.

Diazo answered 19/4, 2010 at 8:33 Comment(2)
This was the closest answer... Changing to object and removing the "=" fixes the immediate problem (I can get past that configuration dialog now, although the program fails to actually run). You may want to edit your answer to reflect that. ThanksOctofoil
I just tried a test project, as above, and had very similar issues. I've created 50+ little scala projects with the only issue being changing the compiler and library jars defaults (from the plugin) to those from the scala distro of choice. However I am seeing the same problem creating a run config, I can compile but the run config dialog fails to find the Hello object, the workaround is not to use the chooser and type "Hello" in the Main class field. There is still a warning about "Main method not found in class Hello" but the run config will work. Contact support, they are very responsive.Diazo
H
2

When you change your implementation from class to object, it works like a charm:

object Hello {
   def main(a: Array[String]) = println("got args: " + a)
}

I picked up this little, but important difference here: http://sonyarouje.com/2011/03/18/running-scala-in-intellij-idea-10/

Hornbill answered 4/3, 2012 at 17:14 Comment(0)
W
1

It may be a bug in the plugin. If you define you object as Hello (capitalized) then it works, at least on my machine.

Woodley answered 19/4, 2010 at 6:41 Comment(0)
J
1

Is your file called hello.scala? (I can't see that it has the .scala extension in your screenshot) - it must be a .scala file as otherwise the compiler will not be able to compile it

Jewel answered 19/4, 2010 at 7:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.