Using Gephi inside a Play Application - scala

I am currently building a Play Framework application in Scala that requires the use of the Gephi Java library.
I tried adding Gephi as a managed dependency in SBT, but SBT cannot resolve all of Gephi's dependencies. This is a known issue. I then simply tried to add the whole Gephi JAR in my lib/ folder as an unmanaged dependency. IntelliJ detects the library just fine and I can compile my application perfectly fine. The problem is an exception occurs immediately upon starting the application, and it seems to be related to dependency injection:
play.api.UnexpectedException: Unexpected exception[NoSuchMethodError: com.google.common.base.Objects.firstNonNull(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;]
at play.core.server.DevServerStart$$anonfun$mainDev$1$$anon$1$$anonfun$get$1$$anonfun$apply$1$$anonfun$1.apply(DevServerStart.scala:174)
at play.core.server.DevServerStart$$anonfun$mainDev$1$$anon$1$$anonfun$get$1$$anonfun$apply$1$$anonfun$1.apply(DevServerStart.scala:126)
at scala.Option.map(Option.scala:146)
at play.core.server.DevServerStart$$anonfun$mainDev$1$$anon$1$$anonfun$get$1$$anonfun$apply$1.apply(DevServerStart.scala:126)
at play.core.server.DevServerStart$$anonfun$mainDev$1$$anon$1$$anonfun$get$1$$anonfun$apply$1.apply(DevServerStart.scala:124)
at scala.util.Success.flatMap(Try.scala:231)
at play.core.server.DevServerStart$$anonfun$mainDev$1$$anon$1$$anonfun$get$1.apply(DevServerStart.scala:124)
at play.core.server.DevServerStart$$anonfun$mainDev$1$$anon$1$$anonfun$get$1.apply(DevServerStart.scala:116)
at scala.concurrent.impl.Future$PromiseCompletingRunnable.liftedTree1$1(Future.scala:24)
at scala.concurrent.impl.Future$PromiseCompletingRunnable.run(Future.scala:24)
Caused by: java.lang.NoSuchMethodError: com.google.common.base.Objects.firstNonNull(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
at com.google.common.cache.CacheBuilder.getKeyStrength(CacheBuilder.java:529)
at com.google.common.cache.LocalCache.<init>(LocalCache.java:242)
at com.google.common.cache.LocalCache$LocalManualCache.<init>(LocalCache.java:4718)
at com.google.common.cache.CacheBuilder.build(CacheBuilder.java:807)
at com.google.inject.internal.WeakKeySet.<init>(WeakKeySet.java:55)
at com.google.inject.internal.InheritingState.<init>(InheritingState.java:67)
at com.google.inject.internal.InjectorShell$Builder.getState(InjectorShell.java:209)
at com.google.inject.internal.InjectorShell$Builder.lock(InjectorShell.java:115)
at com.google.inject.internal.InternalInjectorCreator.build(InternalInjectorCreator.java:103)
at com.google.inject.Guice.createInjector(Guice.java:96)
I tried removing all mention of Gephi in the code, while still having the JAR sitting in my lib/ folder, but it results in the same exception.
Gephi is really important for this project, so I can't just move on to another library. Any ideas?

Remove that jar fom lib, add those two additional resolvers (in build.sbt)
resolvers += "NetBeans" at "http://bits.netbeans.org/nexus/content/groups/netbeans/"
resolvers += "gephi" at "https://raw.github.com/gephi/gephi/mvn-thirdparty-repo/"
and run activator update

Related

`NoClassDefFoundError` in Artifact built with IntelliJ

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.

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.

NoClassDefFoundError for Play app with sbt using Eclipse

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!

UnmanagedJars in sbt plugin

I am creating a sbt plugin. One of its features is to display a dependency graph of a given scala project (the one invoking this plugin). To do so, I use scalafx, which is still depending on a javafx jar. So I need to add in my plugin build.sbt the following command:
unmanagedJars in Compile += Attributed.blank(file(System.getenv("JAVA_ORACLE") + "/jre/lib/jfxrt.jar"))
It compiles and works well. But not when I use it as a plugin from another sbt project (with the addSbtPlugin command in the project/plugins.sbt). When I invoke the plugin, I get the following error:
java.lang.NoClassDefFoundError: javafx/event/EventTarget
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:787)
The javafx classes are not found in the classLoader. I tried the assembly plugin - but it is not packaged yet for scala 2.10 that I use.
Does anyone know how I can render my javafx jar available at runtime when it is invoked by my plugin ?
Thanks
I'm facing the same problem (with a different .jar file) and I found a workaround. I know it's not what we are looking for (A way for the plugin to automatically have that .jar file in its classpath when being used by another project), but it works.
Add the line
unmanagedJars in Compile += {
Attributed.blank(file(System.getenv("JAVA_ORACLE") + "/jre/lib/jfxrt.jar"))
}
to the file yourMainProject/project/plugin.sbt. The same file where you placed the line addSbtPlugin(...) with you plugin.
Hope it works. If you get to find a way to solve it in the nice way, let me know.
Also check this answer, it may be helpful How to generate sources in an sbt plugin?

How to tell sbt-proguard to include java *.jars?

I'm trying to make single executable *.jar via proguard plugin for sbt 10.*.
All seems to be okay, except that sbt-proguard doesn't include java jars (in my case mysql-connector-java-5.1.10.jar) cause when I'm trying to run output jar with
java -jar proguard-output.min.jar
I'm getting
Exception in thread "main" java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
(while sbt `run` goes fine so mysql connector is definitely in project).
I've tried to leverage that problem by adding jar explicitly in build.sbt:
proguardInJars += Path.userHome / ".m2" / "repository" / "mysql" / "mysql-connector-java" / "5.1.10" / "mysql-connector-java-5.1.10.jar"
moreover in proguard log:
...
Preparing output jar
...
Copying resources from program jar [/home/kostya/.m2/repository/mysql/mysql-connector-java/5.1.10/mysql-connector-java-5.1.10.jar] (filtered)
...
But I'm still getting the same exception. What am I doing wrong?
The class is specified in the input, but the code only instantiates it by introspection, which ProGuard can't know. You therefore have to specify explicitly that it has to be preserved in the output.
Cfr. ProGuard manual > Examples > Processing database drivers
Cfr. ProGuard manual > Troubleshooting > ClassNotFoundException
A safer solution is probably not to process third-party jars like the JDBC driver, but to specify them as library jars instead.