Re: [2.1-RC1] java.lang.NoClassDefFoundError running in dev mode (prod mode fine) using Jackson

635 views
Skip to first unread message
Message has been deleted

James Roper

unread,
Dec 4, 2012, 2:19:53 AM12/4/12
to play-fr...@googlegroups.com
I assume you are using the JsonTypeInfo.Id.CLASS strategy right?  Because this is the only place that I can think of that Jackson will ever look anything up from a classloader.  In this case, looking at the Jackson source code, it appears to be using the ContextClassLoader.  While Play does attempt to set the right context classloader at action execution time, at classloading time, Play makes no guarantees as to what the context classloader will be, so your code should make no such assumptions.  To make your code work, wrap it in something like this:

static {
 
ClassLoader old = Thread.currentThread().getContextClassLoader();
 
Thread.currentThread().setContextClassLoader(IntelligenceController.class.getClassLoader());
 
try {
   
// your code here
 
} finally {
   
Thread.currentThread().setContextClassLoader(old);
 
}
}

Note though that talking to a database like this at classloading time is probably not a good idea at all.  It would be much better if you move that to Global.onStart().

On Monday, 3 December 2012 00:50:21 UTC+11, Humphrey Sheil wrote:

Hi

We've upgraded one of our Java applications to 2.1-RC1. We've been mostly successful apart from one issue:

When running in dev mode, classes that live under the app/ folder are no longer found by / available to the Jackson class loader at runtime, resulting in a java.lang.NoClassDefFoundError (see a sample stacktrace below). In this particular use case, the controller has a static block that is executed when the class is first initialised - this static block reads some data from Mongo and attempts to de-serialise it into regular POJOs using Jackson. We're using Jackson 2.0.4 - the POJOs themselves use the @JsonProperty and @JsonTypeInfo annotations imported from com.fasterxml.jackson.annotation.*.

This error does not occur when running in production mode. We're not currently explicitly referencing any classes living under the app/ folder as dependencies of our application in Build.scala - just the javaCore module.

Any tips / pointers appreciated (at the moment I'm planning to dig into any classloader changes made between 2.0.x and 2.1-RC1 as well as any Play <-> Jackson changes in 2.1).

We also found and fixed a few minor config things around getting the jenkins CI server to build our project using sbt 0.12.0 that we'll document and publish.

Humphrey


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

play.api.Application$$anon$1: Execution exception[[RuntimeException: java.lang.NoClassDefFoundError: Could not initialize class controllers.IntelligenceController]]
        at play.api.Application$class.handleError(Application.scala:279) ~[play_2.10.jar:2.1-RC1]
        at play.api.DefaultApplication.handleError(Application.scala:373) [play_2.10.jar:2.1-RC1]
        at play.core.server.netty.PlayDefaultUpstreamHandler$$anon$2$$anonfun$handle$1.apply(PlayDefaultUpstreamHandler.scala:112) [play_2.10.jar:2.1-RC1]
        at play.core.server.netty.PlayDefaultUpstreamHandler$$anon$2$$anonfun$handle$1.apply(PlayDefaultUpstreamHandler.scala:108) [play_2.10.jar:2.1-RC1]
        at play.api.libs.concurrent.PlayPromise$$anonfun$extend1$1.apply(Promise.scala:110) [play_2.10.jar:2.1-RC1]
        at play.api.libs.concurrent.PlayPromise$$anonfun$extend1$1.apply(Promise.scala:110) [play_2.10.jar:2.1-RC1]
Caused by: java.lang.RuntimeException: java.lang.NoClassDefFoundError: Could not initialize class controllers.IntelligenceController
        at play.libs.F$Promise$6.apply(F.java:377) ~[play_2.10.jar:2.1-RC1]
        at scala.concurrent.Future$$anonfun$map$1.liftedTree2$1(Future.scala:253) ~[scala-library.jar:na]
        at scala.concurrent.Future$$anonfun$map$1.apply(Future.scala:251) ~[scala-library.jar:na]
        at scala.concurrent.Future$$anonfun$map$1.apply(Future.scala:249) ~[scala-library.jar:na]
        at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:29) ~[scala-library.jar:na]
        at akka.dispatch.BatchingExecutor$Batch$$anonfun$run$1.liftedTree1$1(BatchingExecutor.scala:67) ~[akka-actor_2.10.0-RC1.jar:na]
Reply all
Reply to author
Forward
0 new messages