有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

使用maven构建java jar显示错误

我构建了一个应用程序,从一些xml文件的Count标记中获取值,并将其写入输出的excel XLSX文件

我的程序中有两个函数被main()调用。第一个是readXml()读取xml文件,第二个是writeXlsx()写入输出excel文件

我使用maven添加依赖项并构建jar文件

当我运行创建的jar时,它在程序的一半时间内运行良好,但在尝试编写XLSX时出现了一个错误。出现的错误是:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/poi/xssf/usermodel/XSSFWorkbook
    at com.mpstddn.App.writeXLSX(App.java:107)
    at com.mpstddn.App.main(App.java:64)
Caused by: java.lang.ClassNotFoundException: org.apache.poi.xssf.usermodel.XSSFWorkbook

当我从eclipse运行这个程序时,它运行得很好,但当我制作一个jar并运行它时,它会出错。在我看来,jar不能包含ApachePOI库。我是马文的新手,不知道自己在哪里犯错。以下是我的^{

<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>com.mpstddn.java</groupId>
    <artifactId>myMavenProject</artifactId>
    <packaging>jar</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>myMaven</name>
    <url>http://maven.apache.org</url>
    <dependencies>
       <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.15</version>
        <exclusions>
            <exclusion>
                <groupId>com.sun.jdmk</groupId>
                <artifactId>jmxri</artifactId>
            </exclusion>
            <exclusion>
                <groupId>javax.jms</groupId>
                <artifactId>jms</artifactId>
            </exclusion>
            <exclusion>
                <groupId>com.sun.jmx</groupId>
                <artifactId>jmxri</artifactId>
            </exclusion>
            <exclusion>
                <groupId>com.sun.jdmk</groupId>
                <artifactId>jmxtools</artifactId>
            </exclusion>
        </exclusions>
      </dependency>
      <dependency>
          <groupId>org.apache.poi</groupId>
          <artifactId>openxml4j</artifactId>
          <version>1.0-beta</version>
      </dependency>     
       <dependency>
          <groupId>org.apache.poi</groupId>
          <artifactId>poi-ooxml</artifactId>
          <version>3.10-FINAL</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-scratchpad</artifactId>
            <version>3.9</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.10-FINAL</version>
        </dependency>
     </dependencies>
    <build>
        <sourceDirectory>src</sourceDirectory>
        <scriptSourceDirectory>src\main\scripts</scriptSourceDirectory>
        <testSourceDirectory>src\test\java</testSourceDirectory>
        <outputDirectory>target\classes</outputDirectory>
        <testOutputDirectory>target\test-classes</testOutputDirectory>
    <resources>
        <resource>
            <directory>src</directory>
            <excludes>
                <exclude>**/*.java</exclude>
            </excludes>
        </resource>
        <resource>
            <directory>lib</directory>
            <excludes>
                <exclude>**/*.java</exclude>
                <exclude>**/*.jar</exclude>
            </excludes>
        </resource>
 </resources>
 <pluginManagement>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.2-beta-5</version>
            </plugin>
            <plugin>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.1</version>
            </plugin>
            <plugin>
                <artifactId>maven-release-plugin</artifactId>
                <version>2.0</version>
            </plugin>
        </plugins>
 </pluginManagement>
  <plugins>
        <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                        <archive>
                                <manifest>
                                        <mainClass>com.mpstddn.App</mainClass>
                                </manifest>
                        </archive>
                </configuration>
            </plugin>
       <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <executions>
                <execution>
                    <id>default-testCompile</id>
                    <phase>test-compile</phase>
                    <goals>
                        <goal>testCompile</goal>
                    </goals>
                </execution>
                <execution>
                    <id>default-compile</id>
                    <phase>compile</phase>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                    <configuration>
                        <source>1.7</source>
                        <target>1.7</target>
                        <encoding>UTF-8</encoding>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <artifactId>maven-clean-plugin</artifactId>
            <version>2.4.1</version>
            <executions>
                <execution>
                    <id>default-clean</id>
                    <phase>clean</phase>
                    <goals>
                        <goal>clean</goal>
                    </goals>
                </execution>
            </executions>
         </plugin>
         <plugin>
            <artifactId>maven-install-plugin</artifactId>
            <version>2.3.1</version>
            <executions>
                <execution>
                    <id>default-install</id>
                    <phase>install</phase>
                    <goals>
                        <goal>install</goal>
                    </goals>
                </execution>
            </executions>
         </plugin>
         <plugin>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.5</version>
            <executions>
                <execution>
                    <id>default-resources</id>
                    <phase>process-resources</phase>
                    <goals>
                        <goal>resources</goal>
                    </goals>
                </execution>
                <execution>
                    <id>default-testResources</id>
                    <phase>process-test-resources</phase>
                    <goals>
                        <goal>testResources</goal>
                    </goals>
                </execution>
            </executions>
         </plugin>
         <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.10</version>
            <executions>
                  <execution>
                    <id>default-test</id>
                      <phase>test</phase>
                     <goals>
                        <goal>test</goal>
                     </goals>
                </execution>
            </executions>
         </plugin>
         <plugin>
             <artifactId>maven-jar-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <archive>
                     <manifest>
                         <addClasspath>true</addClasspath>
                                <classpathPrefix>lib/</classpathPrefix>
                             <mainClass>com.mpstddn.App</mainClass>
                      </manifest>
                 </archive>
            </configuration>
             <executions>
                <execution>
                     <id>default-jar</id>
                    <phase>package</phase>
                    <configuration>
                        <packagingExcludes>*.jar</packagingExcludes>
                    </configuration>
                    <goals>
                        <goal>jar</goal>
                    </goals>
                </execution>
            </executions>
         </plugin>
         <plugin>
            <artifactId>maven-deploy-plugin</artifactId>
            <version>2.7</version>
            <executions>
                <execution>
                    <id>default-deploy</id>
                    <phase>deploy</phase>
                    <goals>
                        <goal>deploy</goal>
                    </goals>
                </execution>
            </executions>
         </plugin>
         <plugin>
          <artifactId>maven-site-plugin</artifactId>
            <version>3.0</version>
            <executions>
                <execution>
                    <id>default-site</id>
                    <phase>site</phase>
                    <goals>
                        <goal>site</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>target\site</outputDirectory>
                        <reportPlugins>
                            <reportPlugin>
                                <groupId>org.apache.maven.plugins</groupId>
                                <artifactId>maven-project-info-reports-plugin</artifactId>
                            </reportPlugin>
                        </reportPlugins>
                    </configuration>
                </execution>
                <execution>
                    <id>default-deploy</id>
                    <phase>site-deploy</phase>
                    <goals>
                        <goal>deploy</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>target\site</outputDirectory>
                        <reportPlugins>
                            <reportPlugin>
                                <groupId>org.apache.maven.plugins</groupId>
                                <artifactId>maven-project-info-reports-plugin</artifactId>
                            </reportPlugin>
                        </reportPlugins>
                    </configuration>
                </execution>
            </executions>
            <configuration>
                <outputDirectory>target\site</outputDirectory>
                <reportPlugins>
                    <reportPlugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-project-info-reports-plugin</artifactId>
                    </reportPlugin>
                </reportPlugins>
            </configuration>
        </plugin>
       </plugins>
     </build>
 </project>

我创建罐子的方式: right click on project in eclipse -> run as -> Maven install 这将在项目文件夹内的target目录中创建一个jar文件

我正在使用命令提示符运行jar文件,方法是键入:

java -jar myMavenProject-0.0.1-SNAPSHOT.jar X:\input_directory input.xml 9

共 (1) 个答案

  1. # 1 楼答案

    因此,您的问题不是基于依赖关系,而是因为您错误地配置了maven assembly插件:

    <project>
      [...]
      <build>
        [...]
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.5</version>
            <configuration>
              <descriptorRefs>
                <descriptorRef>jar-with-dependencies</descriptorRef>
              </descriptorRefs>
            </configuration>
            <executions>
              <execution>
               <id>package</id>
               <goals>
                 <goal>single</goal>
               <goals>
               <phase>package</phase>
              </execution>
            </executions>
          </plugin>
        </plugins>
     </build>
    </project>
    

    问题是maven assembly插件在默认情况下不会绑定到任何生命周期阶段。然后尝试:

    mvn clean package
    

    并检查生成的jar文件(target/WhatEver-x.x-SNAPSHOT-jar-with-dependencies.jar