Jar Maven项目中的ClassNotFoundException

Jar Maven项目中的ClassNotFoundException,jar,maven-3,classnotfoundexception,Jar,Maven 3,Classnotfoundexception,有一个基于maven的项目,可以通过maven安装构建,但在运行完jar时抛出异常: Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/cli/CommandLineParser Caused by: java.lang.ClassNotFoundException: org.apache.commons.cli.CommandLineParser at java.net.URLCl

有一个基于maven的项目,可以通过
maven安装
构建,但在运行完jar时抛出异常:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/cli/CommandLineParser
Caused by: java.lang.ClassNotFoundException: org.apache.commons.cli.CommandLineParser
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
我想知道为什么java找不到maven依赖库

<?xml version="1.0" encoding="UTF-8"?>
<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>org.artofsolving.jodconverter</groupId>
  <artifactId>jodconverter-core</artifactId>
  <version>3.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>JODConverter - Core Library</name>
  <description>
    JODConverter converts office documents using OpenOffice.org
  </description>
  <url>http://jodconverter.googlecode.com</url>
  <licenses>
    <license>
      <name>GNU Lesser General Public License, Version 3 or later</name>
      <url>http://www.gnu.org/licenses/lgpl.html</url>
    </license>
  </licenses>
  <scm>
    <url>http://jodconverter.googlecode.com/svn/trunk/jodconverter-core</url>
    <connection>scm:svn:https://jodconverter.googlecode.com/svn/trunk/jodconverter-core/</connection>
  </scm>
  <inceptionYear>2003</inceptionYear>
  <developers>
    <developer>
      <id>mirko.nasato</id>
      <name>Mirko Nasato</name>
      <email>mirko@artofsolving.com</email>
    </developer>
  </developers>
  <contributors>
    <contributor>
      <name>Shervin Asgari</name>
      <email>shervin@asgari.no</email>
    </contributor>
  </contributors>

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

  <repositories>
    <repository>
      <!-- required for org.hyperic:sigar -->
      <id>jboss-public-repository-group</id>
      <url>https://repository.jboss.org/nexus/content/groups/public-jboss/</url>
    </repository>
  </repositories>

  <dependencies>

    <!-- Required dependencies -->

    <dependency>
      <groupId>commons-io</groupId>
      <artifactId>commons-io</artifactId>
      <version>1.4</version>
    </dependency>
    <dependency>
      <groupId>org.openoffice</groupId>
      <artifactId>juh</artifactId>
      <version>3.2.1</version>
    </dependency>
    <dependency>
      <groupId>org.openoffice</groupId>
      <artifactId>ridl</artifactId>
      <version>3.2.1</version>
    </dependency>
    <dependency>
      <groupId>org.openoffice</groupId>
      <artifactId>unoil</artifactId>
      <version>3.2.1</version>
    </dependency>

    <dependency>
      <!-- for the command line tool -->
      <groupId>commons-cli</groupId>
      <artifactId>commons-cli</artifactId>
      <version>1.2</version>
    </dependency>

    <!-- Optional dependencies -->


    <dependency>
      <!-- for more robust process management -->
      <groupId>org.hyperic</groupId>
      <artifactId>sigar</artifactId>
      <version>1.6.5.132</version>
      <optional>true</optional>
    </dependency>
    <dependency>
      <!-- for JSON configuration -->
      <groupId>org.json</groupId>
      <artifactId>json</artifactId>
      <version>20090211</version>
      <optional>true</optional>
    </dependency>

    <!--  Test dependencies -->

    <dependency>
      <groupId>org.testng</groupId>
      <artifactId>testng</artifactId>
      <version>6.0.1</version>
      <scope>test</scope>
    </dependency>

  </dependencies>

  <build>
    <plugins>
      <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>
          <encoding>UTF-8</encoding>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.7.2</version>
        <configuration>
          <!-- don't run tests in parallel -->
          <perCoreThreadCount>false</perCoreThreadCount>
          <threadCount>1</threadCount>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.3.1</version>
        <configuration>
          <archive>
            <manifest>
              <mainClass>org.artofsolving.jodconverter.cli.Convert</mainClass>
              <addClasspath>true</addClasspath>
            </manifest>
          </archive>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.2-beta-5</version>
        <configuration>
          <descriptors>
            <descriptor>src/main/assembly/dist.xml</descriptor>
          </descriptors>
        </configuration>
      </plugin>
    </plugins>
  </build>

  <reporting>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-javadoc-plugin</artifactId>
        <version>2.7</version>
      </plugin>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>cobertura-maven-plugin</artifactId>
        <version>2.4</version>
      </plugin>
    </plugins>
  </reporting>

</project>

4.0.0
org.artofsolving.jodconverter
jodconverter磁芯
3.0-快照
罐子
JODConverter-核心库
JODConverter使用OpenOffice.org转换office文档
http://jodconverter.googlecode.com
GNU Lesser通用公共许可证,第3版或更高版本
http://www.gnu.org/licenses/lgpl.html
http://jodconverter.googlecode.com/svn/trunk/jodconverter-core
scm:svn:https://jodconverter.googlecode.com/svn/trunk/jodconverter-core/
2003
米尔科·纳萨托
米尔科·纳萨托
mirko@artofsolving.com
谢尔文·阿斯加里
shervin@asgari.no
UTF-8
jboss公共存储库组
https://repository.jboss.org/nexus/content/groups/public-jboss/
公地io
公地io
1.4
org.openoffice
朱
3.2.1
org.openoffice
里德尔
3.2.1
org.openoffice
乌努伊尔
3.2.1
commons cli
commons cli
1.2
org.hyperic
西格尔
1.6.5.132
真的
org.json
json
20090211
真的
org.testng
testng
6.0.1
测试
org.apache.maven.plugins
maven编译器插件
2.3.2
1.6
1.6
UTF-8
org.apache.maven.plugins
maven surefire插件
2.7.2
假的
1.
org.apache.maven.plugins
maven jar插件
2.3.1
org.artofsolving.jodconverter.cli.Convert
真的
org.apache.maven.plugins
maven汇编插件
2.2-β-5
src/main/assembly/dist.xml
org.apache.maven.plugins
maven javadoc插件
2.7
org.codehaus.mojo
cobertura maven插件
2.4

使用
java-Jar
运行Jar时,您不再使用maven解析依赖项。您必须确保您所依赖的所有Jar在Jar中的
MANIFEST.mf
文件中指定的相对位置可用。通常,您会使用assembly插件创建一个可分发的归档文件来创建此结构。或者,您可以使用uberjar/onejar插件来包含Jar中的所有内容。

您有两个选项来解决此问题:

选项1-使用下面的两个插件将依赖项复制到“lib”文件夹(请参见outputDirectory),并让您的可运行JAR知道它们在哪里(请参见classpathPrefix)


org.apache.maven.plugins
maven jar插件
真的
解放党/
example.MyMainClass
org.apache.maven.plugins
maven依赖插件
复制依赖项
包裹
复制依赖项
${project.build.directory}/lib
假的
假的
真的


选项2-使用maven assembly插件创建一个包含所有依赖项的JAR。(在同一个JAR中提取并压缩所有依赖项)


有依赖关系
罐子
假的
/
真的
真的
编译

我也看到了这个问题。Cobertura通过字节码操作创建所有编译类文件(在目标/类中)的检测副本。我的jar文件包含插入指令的类文件(来自target/generated classes/cobertura),但缺少cobertura.jar。因此,ClassNotFoundException是正确的反应

然而,由于我的POM不包含任何提示,这是打包的预期类,Maven可以自由选择。。。并且它使用插入指令的文件集


我的解决方案非常简单:mvn清理和mvn包。第一个调用将删除所有插入指令的类。然后,第二个调用只查找未插入指令的类,而不查找对Cobertura的任何外部引用。

如何运行它?你运行它的文件夹/文件结构是什么?@NilsH
java-jar target/jodconverter-core-3.0-SNAPSHOT.jar
项目是我想我没有把M2_设置在家里:(谢谢,cobertura maven插件可以帮助我。嗨,你能帮我检查另一个类似的问题吗?
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
    <archive>
        <manifest>
            <addClasspath>true</addClasspath>
            <classpathPrefix>lib/</classpathPrefix>
            <mainClass>example.MyMainClass</mainClass>
        </manifest>
    </archive>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
    <execution>
        <id>copy-dependencies</id>
        <phase>package</phase>
        <goals>
            <goal>copy-dependencies</goal>
        </goals>
        <configuration>
            <outputDirectory>${project.build.directory}/lib</outputDirectory>
            <overWriteReleases>false</overWriteReleases>
            <overWriteSnapshots>false</overWriteSnapshots>
            <overWriteIfNewer>true</overWriteIfNewer>
        </configuration>
    </execution>
</executions>
<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">

<id>with-dependencies</id>
<formats>
    <format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
    <dependencySet>
        <outputDirectory>/</outputDirectory>
        <useProjectArtifact>true</useProjectArtifact>
        <unpack>true</unpack>
        <scope>compile</scope>
    </dependencySet>
</dependencySets>