【问题标题】:Run executable jar from command line fails due to missing library由于缺少库,从命令行运行可执行 jar 失败
【发布时间】:2016-05-28 12:41:39
【问题描述】:

我使用以下构建配置从 maven 创建了一个可执行 jar:

<plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.2</version>
    <configuration>
      <source>1.8</source>
      <target>1.8</target>
    </configuration>
  </plugin>
  <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.targetName}/lib</outputDirectory>
                <overWriteReleases>false</overWriteReleases>
                <overWriteSnapshots>false</overWriteSnapshots>
                <overWriteIfNewer>true</overWriteIfNewer>
                <excludeScope>test</excludeScope>
                <includeScope>compile</includeScope>
            </configuration>
        </execution>
    </executions>
  </plugin>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
        <archive>
            <manifest>
                <addClasspath>true</addClasspath>
                <classpathPrefix>lib/</classpathPrefix>
                <packageName>${project.build.packageName}</packageName>
                <mainClass>${project.build.packageName}.${project.build.className}</mainClass>
            </manifest>
            <manifestEntries>
                <Class-Path>.</Class-Path>
            </manifestEntries>
        </archive>
        <finalName>${project.build.targetName}/${project.build.targetName}</finalName>
    </configuration>
  </plugin>

其中生成了以下文件:

./lib/axis-2878297.jar
./lib/axis-wsdl4j-1.5.1.jar
./lib/(other dependencies jars)
./target.jar

1.如果我以这种方式从命令行运行这个jar:

java -jar target.jar

它会产生异常:

Exception in thread "main" java.lang.NoClassDefFoundError: javax/wsdl/OperationType
        at org.apache.axis.description.OperationDesc.<clinit>(OperationDesc.java:59)
        ...

2.如果我以其他方式运行它:

java -cp "target.jar;lib\*" com.test.Main

执行会成功。

P.S.我尝试将 -verbose:class 放入命令中。

对于案例 1,它显示:

[Loaded org.apache.axis.description.OperationDesc from file:/C:/build/lib/axis-2878297.jar]
[Loaded java.lang.Throwable$PrintStreamOrWriter from C:\Program Files\Java\jre1.8.0_74\lib\rt.jar]
[...]

虽然情况 2 显示:

[Loaded org.apache.axis.description.OperationDesc from file:/C:/build/lib/axis-2878297.jar]
[Loaded javax.wsdl.OperationType from file:/C:/build/lib/axis-wsdl4j-1.5.1.jar]
[...]

案例1失败的原因是什么?

谢谢。

【问题讨论】:

  • 查看 target.jar 中的 MANIFEST.MF 文件。 Class-Path 行应明确提及您的所有 lib/* 文件,并使用从 target.jar 开始的正确相对路径名。
  • @ThorbjørnRavnAndersen 感谢您的提示。有趣的是,在 MANIFEST.MF 中,lib 下的所有 jar 都包含在 Class-Path 中,除了 axis-wsdl4j-1.5.1.jar。

标签: java maven jar wsdl classpath


【解决方案1】:

好的,我知道原因了:

在 pom.xml 中,我将 axis-wsdl4j 的范围设置为“已提供”,这表明我希望 JDK 或容器在运行时提供依赖项。所以当 maven 写 manifest.mf 时,它会跳过这个依赖。

感谢所有的答案和帮助。

<dependency>
    <groupId>axis</groupId>
    <artifactId>axis-wsdl4j</artifactId>
    <version>1.5.1</version>
    <scope>provided</scope>
</dependency>

【讨论】:

    【解决方案2】:

    我能够使用Maven shade plugin 完成这项工作。

    我使用了Mykong的教程。

    演示应用是一个使用 Joda 时间的命令行应用:

    package com.mykong.core.utils;
    
    import org.apache.log4j.Logger;
    import org.joda.time.LocalDate;
    
    /**
     * Hello world!
     * @link http://www.mkyong.com/maven/how-to-create-a-jar-file-with-maven/
     */
    public class App {
        public static final Logger LOGGER = Logger.getLogger(App.class);
    
        public static void main(String[] args) {
            System.out.println(getLocalCurrentDate());
        }
    
        private static String getLocalCurrentDate() {
            String result = "";
            try {
                if (LOGGER.isDebugEnabled()) {
                    LOGGER.debug("enter getLocalCurrentDate");
                }
                result = new LocalDate().toString();
            } finally {
                if (LOGGER.isDebugEnabled()) {
                    LOGGER.debug("exit getLocalCurrentDate");
                }
            }
            return result;
        }
    }
    

    这是我的 pom.xml;使其适应您的目的。

    <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>com.mykong.core.utils</groupId>
      <artifactId>dateUtils</artifactId>
    
      <packaging>jar</packaging>
    
      <version>1.0-SNAPSHOT</version>
      <name>dateUtils</name>
      <url>http://maven.apache.org</url>
    
      <properties>
        <jdk.version>1.8</jdk.version>
      </properties>
    
      <dependencies>
        <dependency>
          <groupId>joda-time</groupId>
          <artifactId>joda-time</artifactId>
          <version>2.9.1</version>
        </dependency>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.12</version>
          <scope>test</scope>
        </dependency>
        <dependency>
          <groupId>log4j</groupId>
          <artifactId>log4j</artifactId>
          <version>1.2.17</version>
        </dependency>
      </dependencies>
    
      <build>
        <finalName>dateutils</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.4.3</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <transformers>
                        <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                            <manifestEntries>
                                <Main-Class>com.mykong.core.utils.App</Main-Class>
                                <Build-Number>1</Build-Number>
                            </manifestEntries>
                        </transformer>
                    </transformers>
                </configuration>
            </plugin>
        </plugins>
      </build>
    
        <pluginRepositories>
            <pluginRepository>
                <id>central</id>
                <url>http://repo1.maven.org/maven2/</url>
            </pluginRepository>
        </pluginRepositories>
    </project>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-17
      • 1970-01-01
      相关资源
      最近更新 更多