Search code examples
spring-mvcnoclassdeffounderrorjackrabbit

NoClassDefFoundError accessing jackrabbit jar from servlet


I'm getting the above mentioned error when trying to access a repository using jackrabbit-standalone-2.4.2.jar from a servlet. I didn't use the jackrabbit war because I already have a thick client app working and I want to reuse as much code as possible. I just assumed doing this was possible.

To test I created a small web application. Since I cannot attach a zip file I will just copy the doPost() method below:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    Repository repository = new TransientRepository(
       "repository.xml", //embedded within the war
       "path/to/home/dir");
    Session session = null;
    try {
        session = repository.login();
        System.out.println("root node identifier: " +
        session.getRootNode().getIdentifier());
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        session.logout();
    }
}

When I post to this servlet from an html form the exception has 2 parts:

java.lang.NoClassDefFoundError: javax/jcr/Repository
java.lang.ClassNotFoundException: javax.jcr.Repository

when I add jcr-2.0.jar in it then I get a different error:

java.lang.NoClassDefFoundError: org/apache/jackrabbit/core/TransientRepository

Solution

  • You originally got the ClassNotFoundException because that class wasn't in the classpath. You have fixed that. The NoClassDefFoundError means the class is in the classpath, but there was an problem initializing it. For details about this distinction, see also this question.

    So the class TransientRepository is there, but most likely a class referenced by the TransientRepository isn't. That means most likely you didn't include other required jar files in the classpath. For a complete list of dependencies (required jar files), see the jackrabbit-standalone-2.4.2.jar, or see the Jackrabbit docs. It could also mean you have all jar files, but at least one of the jar files is the wrong version.