找不到类当我尝试运行程序时出现异常

时间:2016-05-20 09:52:21

标签: java maven classnotfoundexception

我使用mvn install下载了java bundle的所有依赖项。然后我编写了测试程序并编译了它。它编译没有错误。但是当我尝试运行它时,会抛出一个运行时异常:

Exception in thread "main" java.lang.NoClassDefFoundError: com/fasterxml/jackson/databind/Module
    at Tester.main(Tester.java:8)
    Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.databind.Module
    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)
    ... 1 more

这是目标目录:

enter image description here

我已将Tester课程立即放在classes目录中。我已经验证了lib目录中存在com/fasterxml/jackson/databind/Module

enter image description here

这可能是什么原因?

以下是java bundle附带的pom.xml

<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>io.swagger</groupId>
<artifactId>swagger-java-client</artifactId>
<packaging>jar</packaging>
<name>swagger-java-client</name>
<version>1.0.0</version>

<prerequisites>
  <maven>2.2.0</maven>
</prerequisites>

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-surefire-plugin</artifactId>
      <version>2.12</version>
      <configuration>
        <systemProperties>
          <property>
            <name>loggerPath</name>
            <value>conf/log4j.properties</value>
          </property>
        </systemProperties>
        <argLine>-Xms512m -Xmx1500m</argLine>
        <parallel>methods</parallel>
        <forkMode>pertest</forkMode>
      </configuration>
    </plugin>
    <plugin>
      <artifactId>maven-dependency-plugin</artifactId>
      <executions>
        <execution>
          <phase>package</phase>
          <goals>
            <goal>copy-dependencies</goal>
          </goals>
          <configuration>
            <outputDirectory>${project.build.directory}/lib</outputDirectory>
          </configuration>
        </execution>
      </executions>
    </plugin>

    <!-- attach test jar -->
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-jar-plugin</artifactId>
      <version>2.2</version>
      <executions>
        <execution>
          <goals>
            <goal>jar</goal>
            <goal>test-jar</goal>
          </goals>
        </execution>
      </executions>
      <configuration>
      </configuration>
    </plugin>

    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>build-helper-maven-plugin</artifactId>
      <version>1.10</version>
      <executions>
        <execution>
          <id>add_sources</id>
          <phase>generate-sources</phase>
          <goals>
            <goal>add-source</goal>
          </goals>
          <configuration>
            <sources>
              <source>src/main/java</source>
            </sources>
          </configuration>
        </execution>
        <execution>
          <id>add_test_sources</id>
          <phase>generate-test-sources</phase>
          <goals>
            <goal>add-test-source</goal>
          </goals>
          <configuration>
            <sources>
              <source>src/test/java</source>
            </sources>
          </configuration>
        </execution>
      </executions>
    </plugin>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>2.3.2</version>
      <configuration>
        <source>1.6</source>
        <target>1.6</target>
      </configuration>
    </plugin>
  </plugins>
</build>
<dependencies>
  <dependency>
    <groupId>io.swagger</groupId>
    <artifactId>swagger-annotations</artifactId>
    <version>${swagger-annotations-version}</version>
  </dependency>

  <!-- HTTP client: jersey-client -->
  <dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-client</artifactId>
    <version>${jersey-version}</version>
  </dependency>
  <dependency>
    <groupId>com.sun.jersey.contribs</groupId>
    <artifactId>jersey-multipart</artifactId>
    <version>${jersey-version}</version>
  </dependency>

  <!-- JSON processing: jackson -->
  <dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>${jackson-version}</version>
  </dependency>
  <dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>${jackson-version}</version>
  </dependency>
  <dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>${jackson-version}</version>
  </dependency>
  <dependency>
    <groupId>com.fasterxml.jackson.jaxrs</groupId>
    <artifactId>jackson-jaxrs-json-provider</artifactId>
    <version>${jackson-version}</version>
  </dependency>
  <dependency>
    <groupId>com.fasterxml.jackson.datatype</groupId>
    <artifactId>jackson-datatype-joda</artifactId>
    <version>2.1.5</version>
  </dependency>
  <dependency>
    <groupId>joda-time</groupId>
    <artifactId>joda-time</artifactId>
    <version>${jodatime-version}</version>
  </dependency>

  <!-- Base64 encoding that works in both JVM and Android -->
  <dependency>
    <groupId>com.brsanthu</groupId>
    <artifactId>migbase64</artifactId>
    <version>2.2</version>
  </dependency>

  <!-- test dependencies -->
  <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>${junit-version}</version>
    <scope>test</scope>
  </dependency>

</dependencies>
<properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <swagger-annotations-version>1.5.8</swagger-annotations-version>
  <jersey-version>1.18</jersey-version>
  <jackson-version>2.4.2</jackson-version>
  <jodatime-version>2.3</jodatime-version>
  <maven-plugin-version>1.0.0</maven-plugin-version>
  <junit-version>4.8.1</junit-version>
</properties>

清单文件:(jackson-databind-2.4.2.jar)

Manifest-Version: 1.0
    Bnd-LastModified: 1407990182364
    Build-Jdk: 1.7.0_55
    Built-By: tatu
    Bundle-Description: General data-binding functionality for Jackson: work
     s on core streaming API
    Bundle-DocURL: http://wiki.fasterxml.com/JacksonHome
    Bundle-License: http://www.apache.org/licenses/LICENSE-2.0.txt
    Bundle-ManifestVersion: 2
    Bundle-Name: jackson-databind
    Bundle-SymbolicName: com.fasterxml.jackson.core.jackson-databind
    Bundle-Vendor: FasterXML
    Bundle-Version: 2.4.2
    Created-By: Apache Maven Bundle Plugin
    Export-Package: com.fasterxml.jackson.databind;version="2.4.2",com.faste
     rxml.jackson.databind.annotation;version="2.4.2",com.fasterxml.jackson.
     databind.cfg;version="2.4.2",com.fasterxml.jackson.databind.deser;versi
     on="2.4.2",com.fasterxml.jackson.databind.deser.impl;version="2.4.2",co
     m.fasterxml.jackson.databind.deser.std;version="2.4.2",com.fasterxml.ja
     ckson.databind.exc;version="2.4.2",com.fasterxml.jackson.databind.ext;v
     ersion="2.4.2",com.fasterxml.jackson.databind.introspect;version="2.4.2
     ",com.fasterxml.jackson.databind.jsonschema;version="2.4.2",com.fasterx
     ml.jackson.databind.jsonFormatVisitors;version="2.4.2",com.fasterxml.ja
     ckson.databind.jsontype;version="2.4.2",com.fasterxml.jackson.databind.
     jsontype.impl;version="2.4.2",com.fasterxml.jackson.databind.module;ver
     sion="2.4.2",com.fasterxml.jackson.databind.node;version="2.4.2",com.fa
     sterxml.jackson.databind.ser;version="2.4.2",com.fasterxml.jackson.data
     bind.ser.impl;version="2.4.2",com.fasterxml.jackson.databind.ser.std;ve
     rsion="2.4.2",com.fasterxml.jackson.databind.type;version="2.4.2",com.f
     asterxml.jackson.databind.util;version="2.4.2"
    Implementation-Build-Date: 2014-08-13 21:22:55-0700
    Implementation-Title: jackson-databind
    Implementation-Vendor: FasterXML
    Implementation-Vendor-Id: com.fasterxml.jackson.core
    Implementation-Version: 2.4.2
    Import-Package: com.fasterxml.jackson.annotation;version="[2.4.0,2.5)",c
     om.fasterxml.jackson.core;version="[2.4.2,2.5)",com.fasterxml.jackson.c
     ore.base;version="[2.4.2,2.5)",com.fasterxml.jackson.core.format;versio
     n="[2.4.2,2.5)",com.fasterxml.jackson.core.io;version="[2.4.2,2.5)",com
     .fasterxml.jackson.core.json;version="[2.4.2,2.5)",com.fasterxml.jackso
     n.core.type;version="[2.4.2,2.5)",com.fasterxml.jackson.core.util;versi
     on="[2.4.2,2.5)",javax.xml.datatype,javax.xml.namespace,javax.xml.parse
     rs,org.w3c.dom,org.w3c.dom.bootstrap,org.w3c.dom.ls,org.xml.sax
    Specification-Title: jackson-databind
    Specification-Vendor: FasterXML
    Specification-Version: 2.4.2
    Tool: Bnd-1.50.0
    X-Compile-Source-JDK: 1.6
    X-Compile-Target-JDK: 1.6

2 个答案:

答案 0 :(得分:0)

您缺少清单文件。 Adding Classes to the JAR File's Classpath

创建清单文件并在其中添加类路径:

Class-Path: jar1-name jar2-name directory-name/jar3-name

要自动生成清单类路径,请使用以下maven插件:

   <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-jar-plugin</artifactId>
      <version>3.0.0</version>
      <configuration>
        <archive>
          <index>true</index>
          <manifest>
            <addClasspath>true</addClasspath>
          </manifest>
        </archive>
      </configuration>
    </plugin>

答案 1 :(得分:-1)

试试这个 - &gt;

  1. 右键点击项目
  2. 点击Build Path - &gt;配置构建路径
  3. 然后在左侧面板中找到Deployement Assembly并单击此
  4. 在右侧点击添加
  5. 单击Java Build Path Entries
  6. 双击Maven Dependencies之后
  7. 点击Apply and Ok
  8. 重新部署您的代码。