Running Scala under Eclipse - eclipse

I'm very new to Scala and I tried to run Scala project under Eclipse. I used sbt to create one, then ran sbteclipse to prepare it for Eclipse and imported it successfully. However when I try to run it I get
Error: Unable to initialize main class Main
Caused by: java.lang.NoClassDefFoundError: scala/Function0
error. Scala, sbt and Java are installed, because when I try to run the same project via console, using sbt, it works. What am I missing?
Thanks for any help!

It looks like the classpath of your Eclipse project is incomplete: it's missing the Scala library. Can you double check in Project Settings that the scala library is present?
If all you want to do is try a simple program, an simpler solution is to create a New Scala Project using the Eclipse wizard.

Related

Intellij: Error while importing SBT project

I used Intellij for 4 months without any problems. Yesterday I installed it on another PC but I can't create a SBT Scala project.
Here the steps:
I create the project:
When started it says me that "SBT project need to be imported" so I click on "Enable Auto-Import" but then I get this error:
If I try to add Scala SDK from modules settings I get this error:
I downloaded Scala and SBT externally, I also tried all the suggested solutions from other similar threads but I can't resolve it.
How can I do?
Thank you in advance!
Add sbt.version to project/build.properties. You can check SBT version using sbt sbtVersion.

Import Twitter/chill to Scala code using Intellij IDE

I have to do some assignments with Scala and I'm a newbie for this language.
In the assignment, the prof requests me to implement serialization and deserialization, using twitter/chill.
https://github.com/twitter/chill/
However, I don't know how to import the libary into my IDE Intellij.
Each time I use val instantiator = new ScalaKryoInstantiator.
IDE notify me that: Cannot resolve symbol ScalaKryoInstantiator.
Could anyone can help me to resolve this issue?
Thanks and Best Regards,
Long.
One way to add a library in a Scala project, if you use sbt, is to add an sbt dependency. Most libraries and jars can be found and downloaded from the maven repositories: https://mvnrepository.com/artifact/com.twitter/chill_2.9.2/0.2.3
If you click the sbt tab on the page above, maven gives you a code snippet which can be directly pasted into your build.sbt. In order to compile and run your project using sbt, open the terminal in the directory where your build.sbt is located and use the "sbt run" command.
If you don't use sbt, download the jar from maven and follow the instructions in this answer: How to add external library in IntelliJ IDEA?

Error while importing/opening project Scala Play IntelliJ

After generating project using (typesafe activator) activator new command I try to open it with IntelliJ IDEA. I meet a problem with importing that is shown below.
What can it be caused by?
I use IDEA 14.1.4 and typesafe activator 1.3.5
Whenever I've seen this error it's because the Java version is incorrect. When you setup the settings for your project, try setting the SDK to 1.8 rather than 1.6.

Intellij IDEA 13: Error compiling sbt component

I have a Maven Scala project created with archetype:
archetypeGroupId=org.scala-tools.archetypes
archetypeArtifactId=scala-archetype-simple
version=1.3
I'm getting this when trying to run a simple main method:
Error compiling sbt component 'compiler-interface-2.8.0.final-51.0'
The Maven build works fine though... I'm new to Scala so this is probably some simple problem.
The good people over at JetBrains had a solution:
Looks like this plugin is quite old and generates pom with an old
scala version. Try 2.10.0
Meant to post this sooner but I forgot about this question.

noClassDefFoundError using Scala Plugin for Eclipse

I successfully implemented and ran several Scala tutorials in Eclipse using the Scala plugin. Then suddenly I tried to compile and run an example, and this error came up:
Exception in thread "main" java.lang.NoClassDefFoundError: hello/HelloWorld
Caused by: java.lang.ClassNotFoundException: hello.HelloWorld
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:315)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:330)
at java.lang.ClassLoader.loadClass(ClassLoader.java:250)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:398)
After this point I could no longer run any Scala programs in Eclipse. I tried cleaning and rebuilding my project, closing and reopening my project, and closing and reopening Eclipse.
Eclipse version number 3.5.2 and Scala plugin 2.8.0
Here is the original code:
package hello
object HelloWorld {
def main(args: Array[String]){
println("hello world")
}
}
If you see this when you attempt to run as a Scala application then the most likely explanation is that your project didn't compile and no class files were generated. Please check whether or not that's the case: look in your project's output folder for hello/HelloWorld.class.
If your project didn't compile that could either be because there's an error which you've missed (and if this error isn't being reported in the Problems view that could be a bug, in which case please open a ticket on Trac) or because you've turned off automatic builds and not done a manual build of your project.
I had the same problem. Project doesn't compile but there are no errors highlighted and AFAIK the code is OK. It seems to be a problem with the Run Configurations.
Solution 1: Delete the existing Run Configuration for your object and create a new one
Solution 2: Create a new object and cut / paste all your code into that file
When running "clean" does not un-hose Eclipse, I next try saving my work, exiting Eclipse, and re-starting. That usually gets things going again, but not always. A few times I've had to update the Scala plugin with a more recent version (I'm using the latest nightly), to get things working again. I doubt that this worked because the new plugin happened to fix the bug, but rather expect that loading the new plugin gives the whole Eclipse-Scala
system a "total reset" that gets it unhosed.
I was getting this problem in a project that combined .java & .scala files.
The solution for me was:
Remove all .java files
Edit the scala code as needed so it compiles without them.
Add the .java files back in.
Edit the scala code back.
The other solutions given here didn't work for me. I tried: clean project, restarting Eclipse, closing-&-opening the project, creating a new .scala file. No joy.
I'm using Eclipse 3.7 (latest stable), Scala IDE 2.0.0 and Scala 2.9 on Ubuntu Linux 11.10.
The symptoms in my case were:
My project was working, but then it stopped compiling for no apparent reason. The IDE didn't show any compilation errors for .scala files, but there were no .class files in the output directory & I got a NoClassDefError if I tried to run anything.
If I created a deliberate error in a .scala file, that did get picked up as a compilation error.
The .java files were registering errors due to the missing scala classes.
I suppose there's probably a boot-strapping bug somewhere in the IDE plugin for .java/.scala mixes. I've done hybrid projects with this setup without problems, so it's only triggered in some situations. I don't know what the trigger is, but once triggered, there's no nice solution.
I had moved my one and only class/object/application to a package, but had not added the package declaration.
sbt compiled and ran fine; eclipse would not
Adding the package declaration at the top of the file fixed it.
Scala 2.8.3 plugin; no compile error
I encountered this error too but after doing the suggestions here (cleaning, deleting Run Configuration etc), I realized that I set the workspace wrongly that is why the class is not being found.
An indication that this is a problem is when the same error occurs when you try to compile a java project.
I encountered this error (compilation worked in sbt but failed in eclipse) when I created a new package object called "common". Deleting the package object in eclipse caused the compile error to go away. There was nothing in it.
I was using sbt-eclipse to build the eclipse project. I'm using scala eclipse 3.0.0-vfinal-20130326-1146-Typesafe.