Search code examples
javaapachemavennoclassdeffounderrorjackrabbit

Jackrabbit FirstHop example java.lang.NoClassDefFoundError


I am trying with Jackrabbit FirstHops example given on Apache site but I am getting below error when trying to run.

S:\mvnapp\FirstHop2>java -cp target\FirstHop2-1.0-SNAPSHOT.jar org.shobhan.jr.Fi
rstHopSB
Exception in thread "main" java.lang.NoClassDefFoundError: javax/jcr/Credentials

    Caused by: java.lang.ClassNotFoundException: javax.jcr.Credentials
            at java.net.URLClassLoader$1.run(Unknown Source)
            at java.security.AccessController.doPrivileged(Native Method)
            at java.net.URLClassLoader.findClass(Unknown Source)
            at java.lang.ClassLoader.loadClass(Unknown Source)
            at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
            at java.lang.ClassLoader.loadClass(Unknown Source)
            at java.lang.ClassLoader.loadClassInternal(Unknown Source)
    Could not find the main class: org.shobhan.jr.FirstHopSB.  Program will exit.

Below is Pom.xml file, i have added the dependencies as provided on apache site.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>org.shobhan.jr</groupId>
  <artifactId>FirstHop2</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>FirstHop2</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

    <dependencies> 
    <!-- The JCR API --> 
        <dependency> 
            <groupId>javax.jcr</groupId> 
            <artifactId>jcr</artifactId> 
            <version>2.0</version> 
        </dependency> 
        <!-- Jackrabbit content repository --> 
        <dependency> 
            <groupId>org.apache.jackrabbit</groupId> 
            <artifactId>jackrabbit-core</artifactId> 
            <version>2.9.0</version> 
        </dependency> 
        <!-- Use Log4J for logging --> 
        <dependency> 
            <groupId>org.slf4j</groupId> 
            <artifactId>slf4j-log4j12</artifactId> 
            <version>1.7.5</version> 
        </dependency> 
    </dependencies> 
</project>

My env. has below values.

CLASSPATH=C:\Program Files\Java\jdk1.6.0_17\bin;S:\JR\jackrabbit-standalone-2.8.0.jar JAVA_HOME=C:\Program Files\Java\jdk1.6.0_17

M2_HOME=S:\maven\apache-maven-3.2.5 PATH=%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;S:\maven\apache-maven-3.2.5\bin;C:\Program Files\Java\jdk1.6.0_17\bin


Solution

  • This error, "NoClassDefFound" happens when a library is present at compile time, but then not in the runtime classpath. It almost always means that the runtime classpath is missing a JAR. You need to include the jcr jar in your classpath if you are hardcoding it.

    A better way is to use the maven exec:java plugin. Update: Here is a gist with a working pom.xml

    Try adding this to your pom.xml, after "depdendencies".

      <build><plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.2.1</version>
        <executions>
          <execution>
            <goals>
              <goal>exec</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <executable>java</executable>
          <includeProjectDependencies>true</includeProjectDependencies>
          <includePluginDependencies>false</includePluginDependencies>
          <classpathScope>compile</classpathScope>
          <mainClass>com.whatever.MyMainClassy</mainClass>
        </configuration>
      </plugin>
      </plugins></build>
    

    you can then run using

    mvn exec:java
    

    and it will use the same classpath as the compile time.

    Testing locally, your output should look like this:

    mvn exec:java
    [INFO] Scanning for projects...
    [INFO]
    [INFO] ------------------------------------------------------------------------
    [INFO] Building FirstHop2 1.0-SNAPSHOT
    [INFO] ------------------------------------------------------------------------
    [INFO]
    [INFO] >>> exec-maven-plugin:1.2.1:java (default-cli) @ FirstHop2 >>>
    [INFO]
    [INFO] <<< exec-maven-plugin:1.2.1:java (default-cli) @ FirstHop2 <<<
    [INFO]
    [INFO] --- exec-maven-plugin:1.2.1:java (default-cli) @ FirstHop2 ---
    Logged in as anonymous to a Jackrabbit repository.
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 5.420s
    [INFO] Finished at: Mon Jan 12 15:27:03 EST 2015
    [INFO] Final Memory: 17M/231M
    [INFO] ------------------------------------------------------------------------