7

でビルドできる Maven ベースのプロジェクトがありますがmaven install、out 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

3 に答える 3

5

これを修正するには、次の 2 つのオプションがあります。

オプション 1 - 以下の両方のプラグインを使用して依存関係を「lib」フォルダーにコピーし (outputDirectory を参照)、Runnable JAR にそれらの場所を認識させます (classpathPrefix を参照)。

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


オプション 2 - maven-assembly-plugin を使用して、すべての依存関係を含む単一の JAR を作成します。(すべての依存関係が抽出され、同じ JAR にまとめて圧縮されます)

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

于 2013-04-19T19:58:18.547 に答える
5

Jar をjava -jarで実行すると、依存関係を解決する Maven で実行されなくなります。依存するすべての jar が、jar 内のMANIFEST.mfファイルで指定された相対的な場所で利用可能であることを確認する必要があります。通常、アセンブリ プラグインを使用して、この構造を作成する配布可能なアーカイブを作成します。または、uberjar/onejar プラグインを使用して、Jar 内にすべてを含めることもできます。

于 2013-04-08T06:40:32.733 に答える
0

私もその問題を見てきました。Cobertura は、バイトコード操作によって、コンパイルされたすべてのクラスファイル (ターゲット/クラス内) の計測済みコピーを作成します。私の jar ファイルには (target/generated-classes/cobertura からの) インストルメント化されたクラス ファイルが含まれていましたが、cobertura.jar がありませんでした。したがって、ClassNotFoundException は正しい反応でした。

ただし、私のPOMにはパッケージ化を目的としたクラスであるヒントが含まれていないため、Mavenには自由な選択があり、インストルメント化されたファイルのセットを使用します。

私の解決策は本当にシンプルでした: mvn clean と mvn package. 最初の呼び出しでは、インストルメント化されたすべてのクラスが削除されます。次に、2 番目の呼び出しは、Cobertura への外部参照を持たないインストルメント化されていないクラスのみを検索します。

于 2016-09-27T14:01:45.700 に答える