Jackrabbit FirstHop 示例 java.lang.NoClassDefFoundError

Jackrabbit FirstHop example java.lang.NoClassDefFoundError

我正在尝试使用 Apache 站点上给出的 Jackrabbit FirstHops 示例,但在尝试 运行.

时出现以下错误
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.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.

下面是 Pom.xml 文件,我添加了 apache 站点上提供的依赖项。

<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>

我的环境。具有以下值。

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:\程序Files\Java\jdk1.6.0_17\bin

此错误 "NoClassDefFound" 发生在编译时存在库但不在 运行 时间类路径中的情况下。它几乎总是意味着 运行time 类路径缺少一个 JAR。如果您对其进行硬编码,则需要将 jcr jar 包含在类路径中。

更好的方法是使用 maven exec:java 插件。更新:Here is a gist with a working pom.xml

尝试在 "depdendencies" 之后将此添加到您的 pom.xml。

  <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>

然后您可以 运行 使用

mvn exec:java

并且它将使用与编译时相同的类路径。

在本地测试,您的输出应该如下所示:

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] ------------------------------------------------------------------------

通过使用 SE 应用程序,这个问题在我的案例中得到了解决。