2017-06-07 176 views
0

在通过Apache POI读取XSSFWorkbook时出现以下错误,但是我正在使用集合版本4.1是最新的,也使用POI依赖的最新版本:在读取excel中的“线程异常”主“java.lang.NoClassDefFoundError:org/apache/commons/collections4/ListValuedMap”时Apache POI selenium

Exception in thread "main" java.lang.NoClassDefFoundError: 
org/apache/commons/collections4/ListValuedMap 
at ReadXLS.readexcel(ReadXLS.java:27) 
at exceldataread.main(exceldataread.java:9) 
Caused by: java.lang.ClassNotFoundException: 
org.apache.commons.collections4.ListValuedMap 
at java.net.URLClassLoader.findClass(URLClassLoader.java:381) 
at java.lang.ClassLoader.loadClass(ClassLoader.java:424) 
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335) 
at java.lang.ClassLoader.loadClass(ClassLoader.java:357) 

但是我使用以下Maven dependencies-

<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>practice</groupId> 
<artifactId>Practice_pblm</artifactId> 
<version>0.0.1-SNAPSHOT</version> 
<properties> 
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
</properties> 
<repositories> 
<repository> 
<id>central</id> 
<name>Central Repository</name> 
<url>http://repo.maven.apache.org/maven2</url> 
<layout>default</layout> 
<snapshots> 
<enabled>false</enabled> 
</snapshots> 
</repository> 
</repositories> 
<pluginRepositories> 
<pluginRepository> 
<id>central</id> 
<name>Central Repository</name> 
<url>http://repo.maven.apache.org/maven2</url> 
<layout>default</layout> 
<snapshots> 
<enabled>false</enabled> 
</snapshots> 
<releases> 
<updatePolicy>never</updatePolicy> 
</releases> 
</pluginRepository> 
</pluginRepositories> 
<build> 
<pluginManagement> 
<plugins> 
<plugin> 
<groupId>org.apache.maven.plugins</groupId> 
<artifactId>maven-compiler-plugin</artifactId> 
<version>3.1</version> 
<configuration> 
<source>1.8</source> 
<target>1.8</target> 
</configuration> 
</plugin> 
</plugins> 
</pluginManagement> 
</build> 
<dependencies> 
<dependency> 
<groupId>org.apache.commons</groupId> 
<artifactId>commons-collections4</artifactId> 
<version>4.1</version> 
</dependency> 
<dependency> 
<groupId>org.seleniumhq.selenium</groupId> 
<artifactId>selenium-java</artifactId> 
<version>3.4.0</version> 
</dependency> 
<dependency> 
<groupId>org.testng</groupId> 
<artifactId>testng</artifactId> 
<version>6.11</version> 
</dependency> 
<dependency> 
<groupId>org.apache.xmlbeans</groupId> 
<artifactId>xmlbeans</artifactId> 
<version>2.6.0</version> 
</dependency> 
<dependency> 
<groupId>org.apache.poi</groupId> 
<artifactId>poi-ooxml</artifactId> 
<version>3.16</version> 
</dependency> 
<dependency> 
<groupId>org.apache.poi</groupId> 
<artifactId>poi-ooxml-schemas</artifactId> 
<version>3.16</version> 
</dependency> 
<dependency> 
<groupId>org.apache.poi</groupId> 
<artifactId>ooxml-schemas</artifactId> 
<version>1.3</version> 
</dependency> 
</dependencies> 
</project> 
+0

怎么样运行“命令mvn依赖:树 - Dverbose“?你能看到'commons-collections4'( - 重复删除)或类似的东西吗?从依赖关系:树中粘贴输出。 –

+0

您是通过jar文件还是eclipse /任何其他IDE执行此操作? –

+0

@Serio Gragera - [警告] pom for org.apache.commons:commons-collections4:jar:4.1是无效的,传递依赖关系将不可用,启用调试日志记录以获取更多详细信息 –

回答

0

将与您pom.xml的是问题;它并没有组装你的jar中的所有依赖项。将maven程序集插件添加到您的pom.xml

<plugin> 
    <artifactId>maven-assembly-plugin</artifactId> 
    <configuration> 
     <finalName>${project.artifactId}</finalName> 
     <descriptorRefs> 
      <descriptorRef>jar-with-dependencies</descriptorRef> 
     </descriptorRefs> 
     <archive> 
      <manifest> 
       <mainClass>{your.package.name.MainClass}</mainClass> 
      </manifest> 
     </archive> 
     <appendAssemblyId>false</appendAssemblyId> 
    </configuration> 
    <executions> 
     <execution> 
      <id>make-assembly</id> 
      <phase>package</phase> 
      <goals> 
       <goal>single</goal> 
      </goals> 
     </execution> 
    </executions> 
</plugin> 

替换{your.package.name.MainClass}与主类。

我已重组您的pom.xml以使其正常工作。请使用下面的pom.xml来构建您的可执行jar文件。

<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>practice</groupId> 
    <artifactId>Practice_pblm</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 
    <packaging>jar</packaging> 
    <properties> 
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
    </properties> 
    <repositories> 
     <repository> 
      <id>central</id> 
      <name>Central Repository</name> 
      <url>http://repo.maven.apache.org/maven2</url> 
      <layout>default</layout> 
      <snapshots> 
       <enabled>false</enabled> 
      </snapshots> 
     </repository> 
    </repositories> 
    <pluginRepositories> 
     <pluginRepository> 
      <id>central</id> 
      <name>Central Repository</name> 
      <url>http://repo.maven.apache.org/maven2</url> 
      <layout>default</layout> 
      <snapshots> 
       <enabled>false</enabled> 
      </snapshots> 
      <releases> 
       <updatePolicy>never</updatePolicy> 
      </releases> 
     </pluginRepository> 
    </pluginRepositories> 
    <build> 
     <plugins> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-compiler-plugin</artifactId> 
       <version>3.1</version> 
       <configuration> 
        <source>1.8</source> 
        <target>1.8</target> 
       </configuration> 
      </plugin> 
      <plugin> 
       <artifactId>maven-assembly-plugin</artifactId> 
       <configuration> 
        <finalName>${project.artifactId}</finalName> 
        <descriptorRefs> 
         <descriptorRef>jar-with-dependencies</descriptorRef> 
        </descriptorRefs> 
        <archive> 
         <manifest> 
          <mainClass>{your.package.name.MainClass}</mainClass> 
         </manifest> 
        </archive> 
        <appendAssemblyId>false</appendAssemblyId> 
       </configuration> 
       <executions> 
        <execution> 
         <id>make-assembly</id> 
         <phase>package</phase> 
         <goals> 
          <goal>single</goal> 
         </goals> 
        </execution> 
       </executions> 
      </plugin> 
     </plugins> 
    </build> 
    <dependencies> 
     <dependency> 
      <groupId>org.apache.commons</groupId> 
      <artifactId>commons-collections4</artifactId> 
      <version>4.1</version> 
     </dependency> 
     <dependency> 
      <groupId>org.seleniumhq.selenium</groupId> 
      <artifactId>selenium-java</artifactId> 
      <version>3.4.0</version> 
     </dependency> 
     <dependency> 
      <groupId>org.testng</groupId> 
      <artifactId>testng</artifactId> 
      <version>6.11</version> 
     </dependency> 
     <dependency> 
      <groupId>org.apache.xmlbeans</groupId> 
      <artifactId>xmlbeans</artifactId> 
      <version>2.6.0</version> 
     </dependency> 
     <dependency> 
      <groupId>org.apache.poi</groupId> 
      <artifactId>poi-ooxml</artifactId> 
      <version>3.16</version> 
     </dependency> 
     <dependency> 
      <groupId>org.apache.poi</groupId> 
      <artifactId>poi-ooxml-schemas</artifactId> 
      <version>3.16</version> 
     </dependency> 
     <dependency> 
      <groupId>org.apache.poi</groupId> 
      <artifactId>ooxml-schemas</artifactId> 
      <version>1.3</version> 
     </dependency> 
    </dependencies> 
</project> 

P.S.:使用Practice_pblm.jar(包括在一个单一的文件相关)为您的执行,而不是Practice_pblm-0.0.1-SNAPSHOT.jar(不包括依存关系)

+0

Krishna:但我没有任何主类,我使用TestNG注释,那么我应该提及什么来代替主类标签。 –

+0

删除清单标签..其添加只是为了您的主要类。 –

+0

Krishna:显示错误“线程中的异常”main“java.lang.NoClassDegfFoundError:org/apache/commons/collections4/ListValuedMap”,我已经更改了我的pom.xml并删除了清单标记。请帮助 –

相关问题