2013-04-08 85 views
5

有一個基於Maven的項目,可以通過maven install建成,但拋出異常時,運行了罐子:的ClassNotFoundException在Maven項目

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>[email protected]</email> 
    </developer> 
    </developers> 
    <contributors> 
    <contributor> 
     <name>Shervin Asgari</name> 
     <email>[email protected]</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> 
+0

你如何運行它?什麼是你運行它的文件夾/文件結構? – NilsH 2013-04-08 04:21:39

+0

@NilsH'Java的罐子目標/ jodconverter核-3.0-SNAPSHOT.jar' – why 2013-04-08 05:41:57

+0

而該項目是https://github.com/mirkonasato/jodconverter – why 2013-04-08 05:43:19

回答

5

當你運行你的Jar與java -jar,你不再運行maven解決依賴關係。您必須確保您所依靠的所有罐子都可以在您瓶子中的MANIFEST.mf文件中指定的相對位置上使用。通常,您將使用程序集插件來創建可分發的存檔,以創建此結構。或者您可以使用uberjar/onejar插件將所有內容都包含在您的Jar中。

+0

謝謝,cobertura- maven插件可以幫助我。 – why 2013-04-08 07:04:03

+0

嗨,你可以請檢查我另一個類似的問題嗎?http://stackoverflow.com/questions/15862628/org-hyperic-sigar-sigarexception-no-libsigar-universal64-macosx-dylib-in -java-1 – why 2013-04-08 07:40:08

4

您有2個選項來解決這個問題:

選項1 - 使用下面這兩個插件的依賴關係複製到「LIB」文件夾中(見輸出目錄),使您的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> 

0

我已經看到了問題,太。 Cobertura通過字節碼操作創建所有已編譯的類文件(在目標/類中)的檢測副本。我的jar文件包含檢測到的類文件(來自target/generated-classes/cobertura),但cobertura.jar丟失。所以,ClassNotFoundException是正確的反應。

但是,由於我的POM不包含任何提示,它們是用於打包的預期類,所以Maven有自由的選擇......並且它使用儀表化的一組文件。

我的解決方案非常簡單:mvn clean和mvn包。第一次調用將刪除所有已插裝的類。然後,第二個調用只找到沒有任何外部引用到Cobertura的uninstrumented類。

相關問題