Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is NoClassDefFoundError thrown with "run" but works fine with "dist"?

Before I asked the question I had read the question Play framework java.lang.NoClassDefFoundError only in dev mode and several other posts. They didn't help me to resolve my problem.

I've created a project using Typesafe Activator 1.2.12 and play-scala template. I also employed Slick 2.1.0.

I want to embed h2 database with h2gis extension into play.

When the Play project is started using activator run, the following Execution exception is displayed in the browser:

enter image description here

The following stack trace is on the console:

18013 [play-akka.actor.default-dispatcher-3] ERROR application -

! @6kmnab7c4 - Internal server error, for (GET) [/2] ->

@6kmnab7c4: Execution exception in null:null
    at play.api.Application$class.handleError(Application.scala:296)
    at play.api.DefaultApplication.handleError(Application.scala:402)
    at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$14$$anonfun$apply$1.applyOrElse(PlayDefaultUpstreamHandler.scala:205)
    at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$14$$anonfun$apply$1.applyOrElse(PlayDefaultUpstreamHandler.scala:202)
    at scala.runtime.AbstractPartialFunction.apply(AbstractPartialFunction.scala:36)
    at scala.util.Failure$$anonfun$recover$1.apply(Try.scala:215)
    at scala.util.Try$.apply(Try.scala:191)
    at scala.util.Failure.recover(Try.scala:215)
    at scala.concurrent.Future$$anonfun$recover$1.apply(Future.scala:324)
    at scala.concurrent.Future$$anonfun$recover$1.apply(Future.scala:324)
    at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:32)
    at play.api.libs.iteratee.Execution$trampoline$.execute(Execution.scala:46)
    at scala.concurrent.impl.CallbackRunnable.executeWithValue(Promise.scala:40)
    at scala.concurrent.impl.Promise$DefaultPromise.tryComplete(Promise.scala:248)
    at scala.concurrent.Promise$class.complete(Promise.scala:55)
    at scala.concurrent.impl.Promise$DefaultPromise.complete(Promise.scala:153)
    at scala.concurrent.impl.Future$PromiseCompletingRunnable.run(Future.scala:23)
    at akka.dispatch.TaskInvocation.run(AbstractDispatcher.scala:41)
    at akka.dispatch.ForkJoinExecutorConfigurator$AkkaForkJoinTask.exec(AbstractDispatcher.scala:393)
    at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260)
    at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339)
    at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979)
    at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107)
Caused by: java.lang.RuntimeException: java.lang.NoClassDefFoundError: com/vividsolutions/jts/io/ParseException
    at play.api.mvc.ActionBuilder$$anon$1.apply(Action.scala:523)
    at play.api.mvc.Action$$anonfun$apply$1$$anonfun$apply$4$$anonfun$apply$5.apply(Action.scala:130)
    at play.api.mvc.Action$$anonfun$apply$1$$anonfun$apply$4$$anonfun$apply$5.apply(Action.scala:130)
    at play.utils.Threads$.withContextClassLoader(Threads.scala:21)
    at play.api.mvc.Action$$anonfun$apply$1$$anonfun$apply$4.apply(Action.scala:129)
    at play.api.mvc.Action$$anonfun$apply$1$$anonfun$apply$4.apply(Action.scala:128)
    at scala.Option.map(Option.scala:145)
    at play.api.mvc.Action$$anonfun$apply$1.apply(Action.scala:128)
    at play.api.mvc.Action$$anonfun$apply$1.apply(Action.scala:121)
    at play.api.libs.iteratee.DoneIteratee$$anonfun$mapM$2.apply(Iteratee.scala:705)
    at play.api.libs.iteratee.DoneIteratee$$anonfun$mapM$2.apply(Iteratee.scala:705)
    at scala.concurrent.impl.Future$PromiseCompletingRunnable.liftedTree1$1(Future.scala:24)
    at scala.concurrent.impl.Future$PromiseCompletingRunnable.run(Future.scala:24)
    ... 6 more
Caused by: java.lang.NoClassDefFoundError: com/vividsolutions/jts/io/ParseException
    at org.h2.value.DataType.convertToValue(DataType.java:1039)
    at org.h2.engine.FunctionAlias$JavaMethod.getValue(FunctionAlias.java:500)
    at org.h2.expression.JavaFunction.getValue(JavaFunction.java:39)
    at org.h2.expression.JavaFunction.optimize(JavaFunction.java:63)
    at org.h2.expression.JavaFunction.optimize(JavaFunction.java:58)
    at org.h2.expression.Function.optimize(Function.java:2104)
    at org.h2.command.dml.Select.prepare(Select.java:831)
    at org.h2.command.Parser.prepareCommand(Parser.java:248)
    at org.h2.engine.Session.prepareLocal(Session.java:442)
    at org.h2.engine.Session.prepareCommand(Session.java:384)
    at org.h2.jdbc.JdbcConnection.prepareCommand(JdbcConnection.java:1188)
    at org.h2.jdbc.JdbcPreparedStatement.<init>(JdbcPreparedStatement.java:73)
    at org.h2.jdbc.JdbcConnection.prepareStatement(JdbcConnection.java:665)
    at scala.slick.jdbc.JdbcBackend$SessionDef$class.prepareStatement(JdbcBackend.scala:152)
    at scala.slick.jdbc.JdbcBackend$BaseSession.prepareStatement(JdbcBackend.scala:389)
    at scala.slick.jdbc.StatementInvoker.results(StatementInvoker.scala:32)
    at scala.slick.jdbc.StatementInvoker.iteratorTo(StatementInvoker.scala:22)
    at scala.slick.jdbc.Invoker$class.foreach(Invoker.scala:64)
    at scala.slick.jdbc.StatementInvoker.foreach(StatementInvoker.scala:16)
    at scala.slick.jdbc.Invoker$class.firstOption(Invoker.scala:29)
    at scala.slick.jdbc.StatementInvoker.firstOption(StatementInvoker.scala:16)
    at scala.slick.jdbc.Invoker$class.first(Invoker.scala:36)
    at scala.slick.jdbc.StatementInvoker.first(StatementInvoker.scala:16)
    at controllers.Application$$anonfun$hi$1$Global$2$$anonfun$onStart$1.apply(Application.scala:45)
    at controllers.Application$$anonfun$hi$1$Global$2$$anonfun$onStart$1.apply(Application.scala:39)
    at scala.slick.backend.DatabaseComponent$DatabaseDef$class.withSession(DatabaseComponent.scala:34)
    at scala.slick.jdbc.JdbcBackend$DatabaseFactoryDef$$anon$4.withSession(JdbcBackend.scala:61)
    at controllers.Application$$anonfun$hi$1$Global$2$.onStart(Application.scala:39)
    at controllers.Application$$anonfun$hi$1.apply(Application.scala:61)
    at controllers.Application$$anonfun$hi$1.apply(Application.scala:27)
    at play.api.mvc.ActionBuilder$$anonfun$apply$17.apply(Action.scala:464)
    at play.api.mvc.ActionBuilder$$anonfun$apply$17.apply(Action.scala:464)
    at play.api.mvc.ActionBuilder$$anonfun$apply$16.apply(Action.scala:433)
    at play.api.mvc.ActionBuilder$$anonfun$apply$16.apply(Action.scala:432)
    at play.api.mvc.Action$.invokeBlock(Action.scala:556)
    at play.api.mvc.Action$.invokeBlock(Action.scala:555)
    at play.api.mvc.ActionBuilder$$anon$1.apply(Action.scala:518)
    ... 18 more
Caused by: java.lang.ClassNotFoundException: com.vividsolutions.jts.io.ParseException
    at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 55 more

However, if I execute activator dist, and run the application it works well (!)

The jar file for h2 is in lib and in sbt's reference, and without using h2gis, no error occurs.

The source code of the project is on GitHub as weird-error project. Use the project to face the error.

Update:

Here is the trace output with command ./activator run -J-XX:+TraceClassLoading I only paste a part of it, it seems CoordinateArraySequence raise the exception.

[Loaded scala.slick.util.ReadAheadIterator$class from file:/Users/myusername/.ivy2/cache/com.typesafe.slick/slick_2.11/bundles/slick_2.11-2.1.0.jar]
[Loaded scala.slick.jdbc.SQLInterpolation$ from file:/Users/myusername/.ivy2/cache/com.typesafe.slick/slick_2.11/bundles/slick_2.11-2.1.0.jar]
[Loaded scala.slick.jdbc.SQLInterpolationResult from file:/Users/myusername/.ivy2/cache/com.typesafe.slick/slick_2.11/bundles/slick_2.11-2.1.0.jar]
[Loaded scala.slick.jdbc.GetResult$GetString$ from file:/Users/myusername/.ivy2/cache/com.typesafe.slick/slick_2.11/bundles/slick_2.11-2.1.0.jar]
[Loaded scala.slick.jdbc.Invoker$$anonfun$firstOption$1 from file:/Users/myusername/.ivy2/cache/com.typesafe.slick/slick_2.11/bundles/slick_2.11-2.1.0.jar]
[Loaded com.vividsolutions.jts.io.WKTReader from file:/Users/myusername/p/scala/weird-error/lib/jts-1.13.jar]
[Loaded com.vividsolutions.jts.geom.impl.CoordinateArraySequence from file:/Users/myusername/p/scala/weird-error/lib/jts-1.13.jar]
[Loaded com.vividsolutions.jts.util.Assert from file:/Users/myusername/p/scala/weird-error/lib/jts-1.13.jar]
[Loaded com.vividsolutions.jts.util.AssertionFailedException from file:/Users/myusername/p/scala/weird-error/lib/jts-1.13.jar]
[Loaded scala.util.Failure$$anonfun$recover$1 from file:/Users/myusername/.ivy2/cache/org.scala-lang/scala-library/jars/scala-library-2.11.1.jar]
[Loaded play.api.Application$$anonfun$4 from file:/Users/myusername/.ivy2/cache/com.typesafe.play/play_2.11/jars/play_2.11-2.3.7.jar]
[Loaded play.core.SourceMapper$$anonfun$sourceFor$1 from file:/Users/myusername/.ivy2/cache/com.typesafe.play/play_2.11/jars/play_2.11-2.3.7.jar]
[Loaded scala.collection.IndexedSeqOptimized$$anonfun$1 from file:/Users/myusername/.ivy2/cache/org.scala-lang/scala-library/jars/scala-library-2.11.1.jar]
[Loaded play.core.ReloadableApplication$$anonfun$get$1$$anonfun$apply$1$$anonfun$1$$anon$1$$anon$2$$anonfun$sourceOf$1 from file:/Users/myusername/.ivy2/cache/com.typesafe.pla
y/play_2.11/jars/play_2.11-2.3.7.jar]
[Loaded scala.Option$$anonfun$orNull$1 from file:/Users/myusername/.ivy2/cache/org.scala-lang/scala-library/jars/scala-library-2.11.1.jar]
[Loaded play.PlayReloader$$anon$1$$anonfun$findSource$1 from file:/Users/myusername/.ivy2/cache/scala_2.10/sbt_0.13/com.typesafe.play/sbt-plugin/jars/sbt-plugin-2.3.7.jar]
[Loaded play.PlayReloader$$anon$1$$anonfun$findSource$1$$anonfun$apply$8 from file:/Users/myusername/.ivy2/cache/scala_2.10/sbt_0.13/com.typesafe.play/sbt-plugin/jars/sbt-plug
in-2.3.7.jar]
[Loaded play.PlayReloader$$anon$1$$anonfun$findSource$1$$anonfun$apply$8$$anonfun$apply$9 from file:/Users/myusername/.ivy2/cache/scala_2.10/sbt_0.13/com.typesafe.play/sbt-plu
gin/jars/sbt-plugin-2.3.7.jar]
[Loaded play.PlayReloader$$anon$1$$anonfun$findSource$1$$anonfun$apply$8$$anonfun$apply$10 from file:/Users/myusername/.ivy2/cache/scala_2.10/sbt_0.13/com.typesafe.play/sbt-pl
ugin/jars/sbt-plugin-2.3.7.jar]
[Loaded play.PlayReloader$$anon$1$$anonfun$findSource$1$$anonfun$apply$11 from file:/Users/myusername/.ivy2/cache/scala_2.10/sbt_0.13/com.typesafe.play/sbt-plugin/jars/sbt-plu
gin-2.3.7.jar]
[Loaded play.PlayReloader$$anon$1$$anonfun$findSource$2 from file:/Users/myusername/.ivy2/cache/scala_2.10/sbt_0.13/com.typesafe.play/sbt-plugin/jars/sbt-plugin-2.3.7.jar]
[Loaded play.core.ReloadableApplication$$anonfun$get$1$$anonfun$apply$1$$anonfun$1$$anon$1$$anon$2$$anonfun$sourceOf$2 from file:/Users/myusername/.ivy2/cache/com.typesafe.pla
y/play_2.11/jars/play_2.11-2.3.7.jar]
[Loaded play.core.SourceMapper$$anonfun$sourceFor$2 from file:/Users/myusername/.ivy2/cache/com.typesafe.play/play_2.11/jars/play_2.11-2.3.7.jar]
[Loaded play.api.Application$$anonfun$handleError$1 from file:/Users/myusername/.ivy2/cache/com.typesafe.play/play_2.11/jars/play_2.11-2.3.7.jar]
[Loaded play.api.Application$$anonfun$handleError$2 from file:/Users/myusername/.ivy2/cache/com.typesafe.play/play_2.11/jars/play_2.11-2.3.7.jar]
[Loaded scala.collection.immutable.StringLike$$anon$1 from file:/Users/myusername/.ivy2/cache/org.scala-lang/scala-library/jars/scala-library-2.11.1.jar]
[Loaded scala.collection.immutable.StringLike$$anonfun$stripMargin$1 from file:/Users/myusername/.ivy2/cache/org.scala-lang/scala-library/jars/scala-library-2.11.1.jar]
18361 [play-akka.actor.default-dispatcher-2] ERROR application -

! @6kpj6pec5 - Internal server error, for (GET) [/2] ->
like image 485
worldterminator Avatar asked Jan 06 '15 11:01

worldterminator


People also ask

How do I fix Java Lang NoClassDefFoundError?

You can fix NoClassDefFoundError error by checking following: Check the exception stack trace to know exactly which class throw the error and which is the class not found by java.

Why am I getting a NoClassDefFoundError in Java?

The NoClassDefFoundError is a runtime error in Java that occurs if the Java Virtual Machine (JVM) or a ClassLoader instance attempts to load the definition of a class that could not be found. The class definition exists at compile-time but is not available at runtime.


1 Answers

Try adding those dependencies to your build.sbt file instead of putting jars inside lib.

"com.typesafe.slick" %% "slick" % "2.1.0",
"org.orbisgis" % "h2gis" % "1.1.0",
"com.vividsolutions" % "jts" % "1.13"

in dev mode, play sbt would use multiple classloaders to support hot-reloading whereas after dist there will only be one classloader to handle your project. With that said, putting stuff all at the same place might solve your issue.

like image 109
NSF Avatar answered Oct 08 '22 23:10

NSF