I've been trying to get a Scala hello world example running on Eclipse using the Scala plugin. Writing the following:
package scala_test
object Test {
def main(args: Array[String]) {
println("Hello, World")
}
}
produces the following error:
<console>:1: error: eof expected but '}' found.
}
^
I've tried this on both a Windows machine and a Mac (both were using Eclipse 3.7). I've tried commenting out the function definition (just leaving an empty body for the Test object) and the error still occurs. Any ideas as to what might cause this?
You need to see output in console, not in scala interpreter.
Just right click on scala file and select "run as scala application".
Make sure you have added the Scala nature to the project. On some versions of the plugin, that option is only available from the contextual "Configuration" menu if the Scala perspective is active. Your project icon should have an "S" instead of a "J" in the corner if you have made this change.
Make sure its in the right package, runs for me. I'm using this plugin
http://scala-ide.org/
Related
I have installed the latest IntelliJ Idea vertion with the Scala plugin. I have created a SBT Scala project with the IDE.
Unfortunately, I am not able to compile or run even the simplest "Hello World" example.
object Main {
def main(args: Array[String]): Unit ={
val x: Int = 5
println("Hello Scala!")
println(x)
}
}
If I try to run it, it says that "Error: could not find main class Main".
Rebuilding the project does not help.
BUT, if I run the "sbt" terminal program and execute "run", everything runs fine. Even more, it compiles the necessary class file so that IntelliJ Idea is able to run it after this step.
But, whenever I change something in the code and try to rebuild it from Idea, it will fail as before.
Edit: in Eclipse everything runs ok.
UPDATE
If I try executing "compile" from the sbt shell and then "Run" with the IDE, it will work. But, it will not do it while executing "Build" or "Rebuild Project". Sometimes, but unfortunately not always sp as to be able to reproduce it, it will throw me an exception with "Could not initialize class sbt.internal.io.Milli$"
UPDATE 2nd:
I have simplified even more the task. Now I have something like the following screenshot:
Screenshot IntelliJ IDEA
UPDATE 3rd:
There were two errors in my deployment. One of them was that, as it was pointed before, there was some package definition problems. All the source code should depend on src/main/scala, but that is not enough. So as to use the SBT structure for construction, I had to go to
File -> Settings -> Build, Execution, Deployment -> Build Tools -> sbt
and check "Use auto-import" and "use sbt shell for build and import".
After that, everything runs ok. Finall!
Thanks a lot tro everyone for the useful input!
On your screenshot Runner is in wrong package. It's written package main.scala.
If Runner.scala is in src/main/scala/way/to/my/package package declaration should be package way.to.my.package (if it's just in src/main/scala there should not be line package ...).
Also if still necessary you can try (from what should be tried first to what should be tried last, if things tried before didn't help)
sbt clean
reimport the project to IntelliJ IDEA
File -> Invalidate Caches / Restart ...
delete .idea subfolder of project folder
Did you try the green arrow near "object Main"?
This should start the program.
I am new to scala. Any help will be much appreciated.
I have created a maven project having scala files in a package in ScalaIDE editor. I want to execute main method implemented as mentioned below in one of the scala file lets say TestHelloWorld.scala
object TestHelloWorld {
def main(args: Array[String]): Unit = {
println("hey .. Hello world !!!")
}
}
I can run it from eclipse run as scala application and print statement be there on eclipse console. But I do not want to run it from IDE.
Please tell me, how should I be running this from command line in a general way, so that it gets executed?
People have suggested to run it through scala prompt "scala > ", but I want to run it from normal console.
I have Intellij-IDEA 13.1.2. [edited, previously 13.0.2]
I use the scala-plugin.
I'm trying to use worksheets to evaluate code.
But all I got are two errors :
bad macro impl binding: versionFormat is supposed to be there
Unable to read an event from: rO0ABXNyADVvcmcuamV0YnJhaW5zLmpwcy5pbmNyZW1lbnRhbC...
I can run the scala console normally and execute my code in it,
but the worksheet doesn't function.
If my code is incorrect, it outputs an error indicating the interpreter failed to parse my code. I got the "bad macro impl binding" error only if my code is correct.
I tried creating a new project, but it didn't work.
I followed tutorial to configure scala in intellij but it didn't help either.
Is there an important configuration step I may have missed ? What does that error mean ?
EDIT : I tried the simplest thing in my worksheet like 1 or var x = 1 or println("Hello World!")
EDIT2: I'm not sure what I changed but now I have another error :
Error:error while loading MacroPrinter, class file needed by MacroPrinter is missing.
reference value macros of package reflect refers to nonexisting symbol.
UPDATE: Now it works fine under Intellij 13.1.5, Scala plugin 0.41.2 with both scala-2.10 and 2.11
I had the "Unable to read an event from" issue and switching from Scala 2.11.0 to Scala 2.10.4 fixed it for me.
I've shared a workaround in a similar question and I think it can work for this question as well:
I'm having the same issue with latest Idea and Scala plugin.
It seems that the worksheet has a problem executing any line which evaluates to Unit. Assigning is Unit, that's why your tableTest(0) = "zero" fails.
I've temporarily solved it with the following workaround:
this line will fail with error Error:Unable to read an event from:...
println("Will fail")
You can fix it by defining this helper method and using it for any Unit expression:
def unit(f: => Unit): String = {f; ""}
unit(println("Will work"))
You just have to ignore the line it generates in the output panel with res0: String =
You also can put this method in some object and import in any WS you need.
I am using IntelliJIDEA 13.1.2 and Scala Plugin 0.36.431. I tried to create a Scala Non-SBT project and created a worksheet file, then I met the "bad macro impl binding" problem. But if I created a Scala SBT project with a worksheet file, it worked well.
After updating to IntelliJ 13.1.5 build 135.1289 with JRE 1.7.0_60 and Scala 2.11.0 I had the same issue.
I fixed the problem by disabling the checkbox in settings -> IDE Settings -> Scala -> "Run compile server".
I have been following the getting started with IntelliJ and Scala video from JetBrains and running into two problems.
I can't get it create or start a run configuration
I don't see the scala-test library as a selection under ProjectStructure-Modules-ChooseLibraries
What I've done so far is
Install Scala, add path and environment variables
Install Scala intellij plugin
Create new project set project sdk to java 1.7 and scala home to /usr/local/share/scala-2.10.3
Create an object that extends from App with a simple write line:
The one source file object
object HelloWorld{
def main(args: Array[String]) {
println("hello")
}
}
In the video they right click on the object file and can see a selection of run, but in my case I only see run as Scala Console. I can't seem to get the debugger to work and when I try to create a run configuration as an "Application" it says the src file is "not acceptable"
I'll recommend my simple project skeleton, to get you quickly up and running with Intellij, SBT and even Eclipse setup. Hope it helps!
Here is a trivial scala script:
object test {
def hi() { print("hi there from here") }
}
test.hi()
From the command line it does the expected:
scala /shared/scaladem/src/main/scala/test.scala
Loading /shared/scaladem/src/main/scala/test.scala...
defined module test
hi there from here
Welcome to Scala version 2.10.2 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_65).
Type in expressions to have them evaluated.
Type :help for more information.
scala>
But within Intellij it gives a compilation error. Right click | Run test.scala
expected class or object definition
test.hi()
^
BTW I also tried running as a scala worksheet. That was MUCH worse - tons of garbage output and did not even get close to compiling.
Update: it appears there is an older but similar question:
How to run Scala code in Intellij Idea 10
I went into Run Configuration and unchecked "Make" as instructed (this was bothersome but so be it ..)
However after making that change I get a different error:
Exception in thread "main" java.lang.NoClassDefFoundError: scala/Either
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:190)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:113)
Caused by: java.lang.ClassNotFoundException: scala.Either
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 3 more
Note: the Scala-Library is properly set up:
Another update (after #lhuang's comment below) I followed the suggestion to create another project from scratch. In that case a Scala Worksheet worked properly (test.sc). But a scala script (which works when running command line via "scala test.scala" ) still does not work, even in this brand new "scala" project.
The answer here is a combination of items:
(a) create a brand new Scala project (as suggested by #lhuang) and
(b) when running a script, you need to go into the Run Configuration and remove the Make step (as mentioned in the 'related' SOF question).
This shows rough edges with Intellij and its scala plugin. Especially when I want to integrate scala with java it is apparently difficult if even possible using Intellij at this time (need to create new Scala project on a frequent basis is a non-starter for mostly java projects attempting to incorporate scala).
But for scala-first projects it seems this may be workable.
What works for me, is:
Write a Scala script, e.g. MyScript.scala
In the menu select: Run -> Edit Configurations...
Press the "+" (⌘N also works on the Mac in this dialog)
Select "Scala Script"
Then select your Script file in this dialog
Now you can run your script.
It's a bit inconvenient, but it works.
Note: This works for me in IntelliJ IDEA 14.x
Your code works in command line because it is "script", when you want to make it runnable in project there are ways to do it:
By creating object which extends App
object test {
def hi() { print("hi there from here") }
}
object runnable extends App {
test.hi()
}
or java-like solution i.e. creating main method
object test {
def hi() { print("hi there from here") }
}
object runnable {
def main(args: Array[String]) {
test.hi()
}
}
when need to execute as script - I do it like that:
select code to execute by mouse, then choose from context menu "Send Selection To Scala Console"
There are 2 things to understand first. Scala works as both interpreter and compiler. You are trying to with interpreter "scala test.scala" without using "scalac test.scala" and interpreter works without main method also.
Other way you can do in intellij is open the terminal or command prompt in intellij and run scala test.scala from the file location (Pre-requisite is scala in present the system path)