I tried many different run configs, but whatever I do I get this exception when running specs2 tests in IntelliJ for scala.
It always fails to find a class that ends with a $ sign. I checked - and there really is no such class file. There's AppControllerIT.class and lots of classes like AppControllerIT$innerFunctionOrclass.clas, but not AppControllerIT$.class
Any ideas?
Thanks!
com.haha.market.api.e2e.controllers.AppControllerIT$
java.lang.ClassNotFoundException: com.haha.market.api.e2e.controllers.AppControllerIT$
STACKTRACE
java.net.URLClassLoader.findClass(URLClassLoader.java:381)
java.lang.ClassLoader.loadClass(ClassLoader.java:424)
sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
java.lang.ClassLoader.loadClass(ClassLoader.java:357)
org.specs2.reflect.Classes$$anonfun$loadClassEither$1.apply(Classes.scala:140)
org.specs2.reflect.Classes$$anonfun$loadClassEither$1.apply(Classes.scala:140)
org.specs2.control.ActionT$$anonfun$safe$1.apply(ActionT.scala:89)
org.specs2.control.ActionT$$anonfun$reader$1$$anonfun$apply$6.apply(ActionT.scala:80)
org.specs2.control.Status$.safe(Status.scala:100)
Classes with $ signs at the end are generate from compiled Scala objects. This means you may have an object defined similar to this:
package com.haha.market.api.e2e.controllers
object AppControllerIT {
}
From your error, it seems that an older compiled artifact or a library (?) is polluting your classpath. First, try cleaning up the project (mvn clean or sbt clean). Next, try to clean any libraries you have in your project inside IntelliJ. IntelliJ sometimes caches multiple versions of the same libraries which may cause confusion during runtime. To clean those up go to "File -> Project Structure" in IntelliJ and manually delete any duplicated libraries you may have.
Related
I use Intellij Idea 2017.3 (Ultimate Edition) to build an artifact (an executable Jar) from a Scala/SBT project; Scala version is 2.12.
Since I have added a dependency to Scallop recently, I can no longer execute the Jar file because the Scallop class ScallopConf is not in the Jar file:
$ java -jar executable.jar
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: org/rogach/scallop/ScallopConf
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
[...]
Caused by: java.lang.ClassNotFoundException: org.rogach.scallop.ScallopConf
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 19 more
I can confirm that the ScallopConf class is not packaged into the Jar file by inspecting it manually. All other dependencies are there, no matter if they were added initially or later.
This is how I added the dependency to the build.sbt file in the project root directory:
libraryDependencies += "org.rogach" %% "scallop" % "3.1.1"
The project compiles fine both within the IDE and with sbt compile. I can also run it fine within the IDE.
I created the artifact within the IDE in a standard way. Is there anything particular I need to pay attention to, possibly related to Scallop?
As pointed out by #Andrey , the artifact settings are not automatically updated when the SBT dependencies change. To make sure everything is up-to-date, the workaround is hence to re-create the artifact after updating the SBT dependencies.
So this issue is not related to the specific dependency (Scallop in this case).
Conflicts are happening between class files of jars, therefore in the above example when libraries are removed from File | Project Structure | Artifacts | Output Layout . Everything runs fine.
In my case I had dependencies on other jars as well, so when I did this activity of removing all other libraries. ClassNotFoundException was gone but NoClassFoundEx is coming for dependent libraries which I removed.
In order to get to the exact solution I am forced to evaluate all the jar files one by one and removed unwanted libraries to get to the exact solution.
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.
I have been trying to use a Jar file as a library in my code, and it compiles fine. However, at runtime, I keep getting the NoClassDefFoundError message. Why is this happening? I have included the Jar file in the compile path and the runtime path too.
Here is the error message:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
at org.apache.pdfbox.cos.COSDocument.(COSDocument.java:51)
at org.apache.pdfbox.pdmodel.PDDocument.(PDDocument.java:136)
at processing.PDFToJPG.main(PDFToJPG.java:58)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 3 more
Here is my code:
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
public static void main(String[] args) {
try {
PDDocument doc = new PDDocument();
} catch (Exception e) {
e.printStackTrace();
}
}
I am using NetBeans IDE as well as Windows 10
This is my setting for compile classpath:
[
This is my setting for runtime classpath:
[
EDIT: Thank you for your help, it really worked. All i needed to do was to download the dependencies Jar file, not editing the classpath like what i have been trying to do
I think you need another jars besides the one you have already included. Try to add common-logging 1.4. Apparently, there is a dependency between pdfbox1.8.jar and this jar as stated on their site.
EDIT: There are more dependencies fontbox and jempbox to take in account as well.
EDIT2: I made a zip with all dependencies needed you can download it here.
I agree with Aurelien's post: it looks like you are missing Apache Commons Logging - and other runtime dependencies.
You might want to consider creating your project as a 'Maven' Project (And Netbeans supports Maven pretty well): and then adding 'pdfbox' as a 'dependency'; this should make life a lot easier for you - since Maven will fetch any other required dependencies.
You can get the 'Maven Coordinates' for the various PDFBox versions from here:
http://mvnrepository.com/artifact/org.apache.pdfbox/pdfbox
If you want to build your final project into a single JAR containing all the deps; or to create a separate 'lib' directory of them: you will have to make some minor changes to the Maven project file ('pom.xml') to do this.
This Stackoverflow Post has an example of doing that.
I am trying to write a Play 2.3.8 application in Scala, managing it via sbt but editing it in Eclipse. I worked round one problem, but this seems to introduce another, and cannot work out how to solve it.
I set up the project using the exact instructions to Create a new application without Activator (except I also add
scalaVersion := "2.11.6"
to build.sbt), then I cd to my project directory, type sbt and once in sbt I type eclipse. Then I open Eclipse and happily import the project.
Now I create a simple template (app/views/Application/index.scala.html) and a controller which calls it (app/controllers/Application.scala). When I go into sbt and type run I can happily open my web browser at localhost:9000 and my populated template appears.
All is good apart from one problem (the first one). When I open up Application.scala in Eclipse I get a wiggly red error line saying "object Application is not a member of package views.html". I solved that using Nick Cooper's answer elsewhere on Stack Overflow. He said to go to Project > Properties > Java Build Path > Libraries > Add Class Folder... and then add target/scala-2.11/classes_managed. That makes the error go away. But I don't want to manage Eclipse's settings directly; I want to manage everything via sbt. So by trial and error I found that I can add this line to my build.sbt file...
unmanagedJars in Compile += ( baseDirectory.value / "target/scala-2.11/classes_managed" )
...and now I can type sbt followed by eclipse and Eclipse's config is generated correctly, with no wiggly red line errors.
But this creates a second problem. It turns out that by introducing that line into build.sbt the application no longer runs. Specifically when I go into sbt, type run and open localhost:9000 I get a NoClassDefFoundError exception in my sbt console:
java.lang.NoClassDefFoundError: controllers/Application$
at Routes$$anonfun$routes$1$$anonfun$applyOrElse$1$$anonfun$apply$1.apply(routes_routing.scala:51) ~[classes_managed/:na]
at Routes$$anonfun$routes$1$$anonfun$applyOrElse$1$$anonfun$apply$1.apply(routes_routing.scala:51) ~[classes_managed/:na]
at play.core.Router$HandlerInvokerFactory$$anon$13$$anon$14.call(Router.scala:217) ~[play_2.11-2.3.8.jar:2.3.8]
at play.core.Router$Routes$TaggingInvoker.call(Router.scala:464) ~[play_2.11-2.3.8.jar:2.3.8]
at Routes$$anonfun$routes$1$$anonfun$applyOrElse$1.apply(routes_routing.scala:51) ~[classes_managed/:na]
Caused by: java.lang.ClassNotFoundException: controllers.Application$
at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[na:1.8.0_40]
at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_40]
at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_40]
at Routes$$anonfun$routes$1$$anonfun$applyOrElse$1$$anonfun$apply$1.apply(routes_routing.scala:51) ~[classes_managed/:na]
at Routes$$anonfun$routes$1$$anonfun$applyOrElse$1$$anonfun$apply$1.apply(routes_routing.scala:51) ~[classes_managed/:na]
[error] application - Error while rendering default error page
scala.MatchError: java.lang.NoClassDefFoundError: controllers/Application$ (of class java.lang.NoClassDefFoundError)
at play.api.GlobalSettings$class.onError(GlobalSettings.scala:148) ~[play_2.11-2.3.8.jar:2.3.8]
at play.api.DefaultGlobal$.onError(GlobalSettings.scala:206) [play_2.11-2.3.8.jar:2.3.8]
at play.core.server.Server$class.logExceptionAndGetResult$1(Server.scala:63) [play_2.11-2.3.8.jar:2.3.8]
at play.core.server.Server$$anonfun$getHandlerFor$4.apply(Server.scala:73) [play_2.11-2.3.8.jar:2.3.8]
at play.core.server.Server$$anonfun$getHandlerFor$4.apply(Server.scala:71) [play_2.11-2.3.8.jar:2.3.8]
You can see the entire (tiny) codebase on Github.
All the class files seem to be all there, and in exactly the same locations, regardless of whether or not I include the "unmanagedJars" line. It's not an Eclipse problem because it happens even when Eclipse is closed. It seems to be a classpath issue (but I can't understand why adding to a classpath should hide some classes). Regardless of that, I'd like to manage my project with sbt and use Eclipse just as the editor. What am I getting wrong?
I have found a solution through further trial and error. Instead of extending the classpath to "target/scala-2.11/classes_managed" it should be extended to "target/scala-2.11/classes". In other words the line in build.sbt should be
unmanagedJars in Compile += ( baseDirectory.value / "target/scala-2.11/classes" )
Now from sbt I can compile, test and run the application, and connect successfully to localhost:9000, and I can also open the files in Eclipse without seeing error lines.
I still don't know why the NoClassDefFoundError really occurred in the previous setup, but that's a problem for another day.
I am using activator but I guess it might be the same.
So, Eclipse does not really like structural changes in a Play project or adding/removing libraries (via libraryDependencies, etc) and displays red squiggly things everywhere. This is how I solve them:
activator clean compile
If I have added/removed libraries, I run
activator eclipse
so that Eclipse gets the changes.
It works everytime. Clean, clean, clean. I actually found this solution somewhere on the Net a while a go but cannot remember where, sorry.
EDIT
And refresh the project in Eclipse!
I'm trying to run tests for an sbt-based Scala application in intellij. But I get the following error which I'm not sure how to fix:
Testing started at 21:07 ...
java.lang.IncompatibleClassChangeError: Found class scala.collection.mutable.ArrayOps, but interface was expected
at org.scalatest.tools.Runner$.checkArgsForValidity(Runner.scala:895)
at org.scalatest.tools.Runner$.runOptionallyWithPassFailReporter(Runner.scala:729)
at org.scalatest.tools.Runner$.run(Runner.scala:711)
at org.scalatest.tools.Runner.run(Runner.scala)
at org.jetbrains.plugins.scala.testingSupport.scalaTest.ScalaTestRunner.runScalaTest2(ScalaTestRunner.java:144)
at org.jetbrains.plugins.scala.testingSupport.scalaTest.ScalaTestRunner.main(ScalaTestRunner.java:35)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
When I decorate my classes with the Junit runner attribute they work fine (and it's actually a much better run in terms of the visuals):
#RunWith(classOf[JUnitRunner])
I got the same error when trying to run tests for my Play Framework app. Drilling down into the stack trace, I found that the problem class was FakeRequest, which is in the play-test library. I had two different versions of the library, one for Play 2.4 and one for 2.3. I was able to resolve this issue by removing the play-test version for Play 2.3 (open Module Settings -> Libraries -> find and delete the bad dependency).
Your issue is probably with some other problematic dependency, but following the same steps as above may help fix it.
This seems to be a problem with scala test runner framework. I had come across the same problem; eventually like you suggested end up using junit test runner to make it work. But the problem in my case was it was pulling in a transitive dependency and no such class error.
Make sure the libraries what you are using for the JUnitRunner are same. Most of the times “Incompatible Class Change Error” occurs because of backward compatibility. And also have a look at scala library jar at the time of compiling and running.