Scala unit-tests in IntelliJ fail with java.lang.NoClassDefFoundError scoverage/Invoker$ - scala

Tests run without any problem in sbt, but when you need them in IntelliJ, they fail.
A needed class was not found. This could be due to an error in your runpath.
Missing class: scoverage/Invoker$
java.lang.NoClassDefFoundError: scoverage/Invoker$
I have seen many suggesting during the years how to fix it, but nothing really helped. :)

Finally discovered a solution, which worked for me:
Temporarely disable scoverage plugin for sbt project(s) in questions
lazy val pipeline: Project = project
...
.disablePlugins(ScoverageSbtPlugin) // <-- temp
Bonus: tests are running faster as well.

Just perform a rebuild (Build -> Rebuild Project).
Intellij is probably storing some temporary/cached files and rebuilding clears them.

Related

Error running Scala tests in Intelli-J

I'm having some problems getting my Scala tests running via the Intelli-J Run/Debug configuration. The tests are working if I run them directly in the SBT console.
My configuration looks like this:
I'm getting this error in the Run console panel:
java.lang.IllegalArgumentException: ERROR: -r has been deprecated for a very long time and is no longer supported, to prepare for reusing it for a different purpose in the near future. Please change all uses of -r to -C.
at org.scalatest.tools.ArgsParser$.checkArgsForValidity(ArgsParser.scala:41)
at org.scalatest.tools.Runner$.runOptionallyWithPassFailReporter(Runner.scala:857)
at org.scalatest.tools.Runner$.run(Runner.scala:850)
at org.scalatest.tools.Runner.run(Runner.scala)
at org.jetbrains.plugins.scala.testingSupport.scalaTest.ScalaTestRunner.runScalaTest2(ScalaTestRunner.java:141)
at org.jetbrains.plugins.scala.testingSupport.scalaTest.ScalaTestRunner.main(ScalaTestRunner.java:32)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
I've checked my plugins are all up-to-date. Do I need to use a particular version of Scala? Or is there some additional setup I'm missing?
For anyone who has this problem even with newer version of IntelliJ, here is how I went about figuring out where the issue was. I pulled in the scalatest dependency at the top of the pom for the module I was trying to run tests in. Then an individual test would run without this error. I moved the dependency down the pom until the problem recurred to figure out which dependency was causing the problem. Then I found that the dependency had some really old dependencies including an older version of scalatest that weren't showing up in my dependency tree. Also, this jar dependency had scalatest as a dependency not marked with scope test.
It appears to have been a problem with the Scala plugin under Intelli-J 13.
I fixed this by upgrading to Intelli-J 2016.3, which I presume has been changed to pass the newer -C switch to ScalaTestRunner.
#JasonF pointed out in his comment below that a project dependency can also cause a problem with the scalatest plugin (this was the case for him). It's worth attempting to run the tests of a fresh sample Scala project to test for this scenario before upgrading the IDE.

Unit tests using Spark do not run any more from IntelliJ IDEA

I had to re-import my SBT project into IDEA, cleaned it thoroughly (git clean -fdx) and even did an "Invalidate caches / restart". Now, all my unit tests using the Spark context fail with:
java.lang.SecurityException: class "javax.servlet.FilterRegistration"'s signer information does not match signer information of other classes in the same package
I already searched and dug around and found out that I have to exclude dependencies on javax.servlet on my dependencies on hadoop-common. I did this, but it did not help.
When running the tests from the console with sbt test, everything runs fine.
What can I additionally check?
EDIT: I even re-imported everything from scratch, including deletion of the .ivy2 folder, and it did not help.
I had to add the exclude for javax.servlet to ALL my Hadoop dependencies!

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.

How to fix NoSuchMethodError?

I use Scala 2.10.0RC1 and sbt 0.12.1.
What causes and how can I fix this runtime error (runs fine on 2.9.2)?
The exact error message is:
java.lang.NoSuchMethodError: scala.Predef$ArrowAssoc$.extension$$minus$greater(Ljava/lang/Object;Ljava/lang/Object;)Lscala/Tuple2;
You're running the code with the wrong Scala version. This can have several causes:
misconfiguration of the project with sbt – search for 2.9.2 in config files
stale cache used by sbt – sbt reboot
something else?
If you meticulously check all your sbt configuration files for 2.9.2 and then wipe out all caches, things should run better. Dependencies usually have a version number in the name of the jar file, so running a find on your system will likely point you to the ones you missed.

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.