【问题标题】:How to make jar file independent?如何使jar文件独立?
【发布时间】:2016-06-02 15:58:03
【问题描述】:

请问,您能解释一下为什么我的 jar 文件没有在目标文件夹之外执行吗?我怎样才能独立完成?将其复制/粘贴到另一个目录。

当我在目标文件夹外执行我的 jar 时,会产生 NoClassDefFound 错误。它无法从依赖项中加载 jar。

这是我的 pom.xml:

<properties>
        <docx4j.version>3.3.0</docx4j.version>
    </properties>

    <build>
        <plugins>
            <plugin>
                <!-- Build an executable JAR -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <mainClass>com.epam.executor.Main</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <!-- Build an executable JAR with runtime dependencies so that this program can be executed from command line using java -jar command -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.10</version>
                <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>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>org.docx4j</groupId>
            <artifactId>docx4j</artifactId>
            <version>${docx4j.version}</version>
        </dependency>
    </dependencies>

我通过这个带有 3 个参数的命令执行我的 jar:

java -jar DocumentTemplate-1.0.jar D:\Trash\xml1.xml D:\Trash\template.docx D:\Trash\results.docx

【问题讨论】:

  • 我猜你需要docx4j 所以你应该把它添加到你的类路径中。您已经添加了lib/,这需要包含您所依赖的所有 jar。 (相对于罐子)
  • 简而言之,docx4j在哪里?你得到的错误信息是什么?

标签: java maven jar


【解决方案1】:

我认为您需要创建一个包含所有可执行依赖项的 jar。我一直是你的单罐插件。试试下面的插件。

  <plugin>
    <!-- Build an executable JAR -->
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.6</version>
    <configuration>
      <archive>
        <manifest>
          <addClasspath>true</addClasspath>
          <classpathPrefix>lib/</classpathPrefix>
          <mainClass>com.kulhade.elasticsearch.Indexer</mainClass>
        </manifest>
      </archive>
    </configuration>
  </plugin>
  <plugin>
    <groupId>org.dstovall</groupId>
    <artifactId>onejar-maven-plugin</artifactId>
    <version>1.4.4</version>
    <executions>
      <execution>
        <goals>
          <goal>one-jar</goal>
        </goals>
      </execution>
    </executions>
  </plugin>

【讨论】:

    【解决方案2】:

    将依赖项复制到特定目录

    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-dependency-plugin</artifactId>
      <executions>
        <execution>
          <id>copy-dependencies</id>
          <phase>prepare-package</phase>
          <goals>
            <goal>copy-dependencies</goal>
          </goals>
          <configuration>
            <outputDirectory>${project.build.directory}/${project.build.finalName}.lib</outputDirectory>
          </configuration>
        </execution>
      </executions>
    </plugin>
    

    使 Jar 可执行且可感知类路径

    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-jar-plugin</artifactId>
      <configuration>
        <archive>
          <manifest>
            <addClasspath>true</addClasspath>
            <classpathPrefix>${project.build.finalName}.lib/</classpathPrefix>
            <mainClass>${fully.qualified.main.class}</mainClass>
          </manifest>
        </archive>
      </configuration>
    </plugin>
    

    此时,jar 实际上可以使用外部类路径元素执行。

    $ java -jar target/${project.build.finalName}.jar
    

    【讨论】:

      猜你喜欢
      • 2022-08-04
      • 1970-01-01
      • 2012-08-27
      • 1970-01-01
      • 2017-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多